• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

initialization of static variables

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
----------------------------------------------------------------------------
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
His brain is the size of a cherry pit! About the size of this ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic