Originally posted by mmkris_1:
hi nikita
I think this is a case of forward referencing.
So here compiler doesn't know that get is existing. So it cannot initialize. But still it is showing 0 because all the object members are initialized to binary zero when an object is created.
Hence you are getting 0.
murali
If you modify the code slightly to be
public class Test1
{
private int j = 10;
private int getJ()
{
return j;
}
private int i = getJ();
public static void main(String[] args)
{
System.out.println(new Test1().i);
}
}
The output is 10
if you modify the code to be
public class Test1
{
private int j = 10;
private int i = getJ();
private int getJ()
{
return j;
}
public static void main(String[] args)
{
System.out.println(new Test1().i);
}
}
The output is still 10!
It isn't that it can't call the method to initialize the variable, it's the order that operations occur. In your example, when i is intialized to getJ(), the value of j is still the default value of 0.
[This message has been edited by Carl Trusiak (edited June 20, 2000).]