Hi friends, please have a look at the foll. codes.
Output: 0 Why there is no compiler error in the second code? Is there is a rule that method calls can forward reference member variables, but a member variable cannot forward reference to another member variable? Also, why is i initialized to 0 instead of 10? Could someone please explain the sequence of events leading to the above output. Thank you, Prasanna. PS: There is no change in output even if we remove the private modifiers from the variables and the method. So it is not related to the method and variables being private.
Cindy Glass
"The Hood"
Sheriff
Joined: Sep 29, 2000
Posts: 8521
posted
0
You tried to access j in the middle of initialization. (be careful - the following reads like greek to me)
8.3.2.3 Restrictions on the use of Fields during Initialization The declaration of a member needs to appear before it is used only if the member is an instance (respectively static) field of a class or interface C and all of the following conditions hold: The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer of C. The usage is not on the left hand side of an assignment. C is the innermost class or interface enclosing the usage. A compile-time error occurs if any of the three requirements above are not met. These restrictions are designed to catch, at compile time, circular or otherwise malformed initializations. Accesses by methods are not checked in this way.
Cindy's translation of the above:
If you have a field that is inside an instance but not nested in any deeper scope, and you want to use the variable on the rightmost side of an assignment - YOU MUST DECLARE THE VARIABLE BEFORE READING IT'S VALUE (unless of course you use a method to get at it early).
You used a method to get at the variable, therefore you were allowed to forward reference, however you got the value of j at that point, which was BEFORE it was initialized to 10, so it still has it's default value. [This message has been edited by Cindy Glass (edited May 18, 2001).]
"JavaRanch, where the deer and the Certified play" - David O'Meara
mousami bhattacharya
Ranch Hand
Joined: May 13, 2001
Posts: 40
posted
0
This is the Example given in JLS.When u try to compile the code it gives lots of forward referencing error.So seems like the rules defined in the JLS are incorrect .So what are the right rules.Anyone??? class UseBeforeDeclaration { static { x = 100; // ok - assignment int y = x + 1; // error - read before declaration int v = x = 3; // ok - x at left hand side of assignment int z = UseBeforeDeclaration.x * 2; // ok - not accessed via simple name Object o = new Object(){ void foo(){x++;} // ok - occurs in a different class {x++;} // ok - occurs in a different class }; } { j = 200; // ok - assignment j = j + 1; // error - right hand side reads before declaration int k = j = j + 1; int n = j = 300; // ok - j at left hand side of assignment int h = j++; // error - read before declaration int l = this.j * 3; // ok - not accessed via simple name Object o = new Object(){ void foo(){j++;} // ok - occurs in a different class { j = j + 1;} // ok - occurs in a different class }; } int w = x= 3; // ok - x at left hand side of assignment int p = x; // ok - instance initializers may access static fields static int u = (new Object(){int bar(){return x;}}).bar(); // ok - occurs in a different class static int x; int m = j = 4; // ok - j at left hand side of assignment int o = (new Object(){int bar(){return j;}}).bar(); // ok - occurs in a different class int j; }
Prasanna Wamanacharya
Ranch Hand
Joined: Apr 24, 2001
Posts: 143
posted
0
Thank you Cindy for the excellent explanation. I have got the concept now. But the example pointed to by Mousami is a mess! It gave 11 compile-time errors relating to forward referencing. The example is given in section 8.3.2.3(Restrictions on the use of Fields during Initialization) of the JLS 2.0. I am giving the code here again in a more readable format. (Mousami, when providing code examples in a posting, please enclose them with the code tags. i.e within . It makes the code more readable )
Cindy, could you please have a look at the code, and explain what is happening? Thank you, Prasanna.
Cindy Glass
"The Hood"
Sheriff
Joined: Sep 29, 2000
Posts: 8521
posted
0
Sun wrote the code on purpose to be full of errors and commented them with the reasons why they have errors just to give you examples of things that will NOT work. Trying to compile such an example is sort of pointless. It was meant to be READ not executed.
Prasanna Wamanacharya
Ranch Hand
Joined: Apr 24, 2001
Posts: 143
posted
0
Thank you Cindy. As always, you came out with an excellent answer. I sat for the test on 21st May 2001, and passed with 89%, thanks to all the helpful people like yourself. Javaranch has added one more feather to its cap. I think that Javaranch has so many feathers now, that entire caps can be made from them! Thanks again, Prasanna. SCJP2