• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

initialization of static variables

 
nowait shen
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dhanashree Mankar
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nowait shen
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic