Variable initialized but javac presents compilation error

Felipe Barros Pontes felipepontes at copin.ufcg.edu.br
Tue Jun 7 13:48:43 UTC 2016


Hi all.

   The Java Language Specification 8 presents in section 8.3.1.2
(final Fields) the following note: "A blank final instance variable
must be definitely assigned at the end of every constructor of the
class in which it is declared, or a compile-time error occurs..."

   In the following program the variable "x" is initialized at the end
of the constructor but javac presents a compilation error.

Program

	class A {
		final int x;
		A() {
			setX();
		}
		void setX() {
			this.x = 10;
		}
	}

Result

A.java:7: error: cannot assign a value to final variable x
			this.x = 10;
			    ^
1 error

   Shouldn't it be compiled successfully?


Best regards,

Felipe Pontes
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20160607/110d8257/attachment.html>


More information about the compiler-dev mailing list