GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes initialization of static variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "initialization of static variables" Watch "initialization of static variables" New topic
Author

initialization of static variables

nowait shen
Greenhorn

Joined: Jul 27, 2003
Posts: 11
How strange these two examples are!!!
Would anybody like to explain how these two examples happen? and why the second example compiles without saying "illegal forward reference?
----------------------------------------------------------------
Example1:
public class MyClass
{
private static int x = y;
private static int y = 5;

private static int getValue()
{
return y;
}

public static void main(String args[])
{
System.out.println(x);
}
}
Result: compiler error, compiler says illegal forward reference
----------------------------------------------------------------------------
Example2
public class MyClass
{
private static int x =getValue();
private static int y = 5;

private static int getValue()
{
return y;
}

public static void main(String args[])
{
System.out.println(x);
}
}
Result: print "0"
----------------------------------------------------------------------------
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch.
from
JLS 8.3.2.3 the 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.
.
.
.
Accesses by methods are not checked in this way.


The usage of y is not within an initializer but in a method. The compiler will not apply the restriction of declaration before usage if usage is in a method.


SCJP2. Please Indent your code using UBB Code
Dhanashree Mankar
Ranch Hand

Joined: Aug 25, 2003
Posts: 123
I don't understand in example 2 how the method getValue() when referring variable y results into default value i.e it returns o.
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
This is becasue static variables/blocks are initialized
by the order they appear.
when getValue() was accessed to initialize static x,
static y still has not being assigned to 5 (y appear under x)
so x got the default value 0.
if you swap the oder of static x and static y like
private static int y = 5;
private static int x =getValue();
then the output will be 5.
Originally posted by dhanashree:
I don't understand in example 2 how the method getValue() when referring variable y results into default value i.e it returns o.


not so smart guy still curious to learn new stuff every now and then
nowait shen
Greenhorn

Joined: Jul 27, 2003
Posts: 11
thanks Jose! that's a great help!
I read the JLS 8.3.2.3 and seemed to understand it a little better, though it took me quite much much time to read those profound words and sentences
 
GeeCON Prague 2014
 
subject: initialization of static variables