Community
Participate
Working Groups
The compiler incorrectly allows writes to final fields, within the constructor for the class declaring a field, when the access is not of the proper form "f = ", or "this.f =". For example, in the following code: public class FinalBug { // incorrectly passes compilation static class Test5 { private final Object o; Test5() { Test5 other = this; other.o = new Object(); // illegal! other.o is not assignable } // error: this.o is not definitely assigned } // flags wrong statement as error static class Test6 { private final Object o; static Test6 initing; Test6() { initing = this; System.out.println("greetings"); Test6 other = initing; other.o = new Object(); // illegal! other.o is not assignable o = new Object(); // legal } } } There should be an error flagged within class Test5, on the marked line, and none is flagged. Also field o is not "definitely assigned" (in terms of the language specification's meaning of "definitely assigned") at the end of the constructor of Test5, and that is also an error that should be flagged. Likewise, in Test6, the assigment to other.o is illegal. Eclipse incorrectly flags the assigment to o as illegal.
Created attachment 46788 [details] final field initialization test cases
Reproduced using I20070320-0010.
Created attachment 62485 [details] Proposed patch
Raising severity. This is quite bad in the compiler to miss this scenario. Surprisingly it never got discovered until now... broken since day 1.
Created attachment 62503 [details] Better patch
Released for 3.3M7
Created attachment 62551 [details] Proposed patch for 3.2.2
Released in 3.2 maintenance stream.
Verified for 3.3M7 with I20070427-0010
*** Bug 190391 has been marked as a duplicate of this bug. ***