java - Using this.var during var's initialization -
this question has answer here:
while researching another question, surprised discover following java code compiles without errors:
public class clazz { int var = this.var + 1; }
in jdk6, var
gets initialized 1
.
does above code have well-defined semantics, or behaviour undefined? if it's well-defined, please quote relevant parts of jls.
it mentioned in passing in example 8.3.2.3-1 in section 8.3.2.3. in text example
class z { static int peek() { return j; } static int = peek(); static int j = 1; } class test { public static void main(string[] args) { system.out.println(z.i); } }
the caption says:
... variable initializer uses class method peek access value of variable j before j has been initialized variable initializer, @ point still has default value (§4.12.5).
this should map directly situation.
Comments
Post a Comment