wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Forward Referencing to method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Forward Referencing to method" Watch "Forward Referencing to method" New topic
Author

Forward Referencing to method

Yogesh Chhawasaria
Ranch Hand

Joined: Apr 02, 2004
Posts: 53

Why does this doesnt give forward reference error and how is the value 0 printed out.
I've been preparing so long time but such ques always take me back to Square One !!


When you have eliminated all which is impossible, then whatever remains, however improbable, must be the truth.
pallavi utukuri
Ranch Hand

Joined: Feb 10, 2004
Posts: 182
first i is initialized by calling giveMej() at this point j is not initialized therefore default value 0 is taken for j and the same is returned.
private int i = giveMeJ();
private int j = 10;
private int giveMeJ()
{
return j;
}
public static void main(String args[])
{
System.out.println((new AQuestion()).i);
}

now if we change like this
private static int j = 10;
first j is initialized to 10 as static variables are first initialized then giveMej() is called as the value of j which is 10 is returned and stored in i. so the output will be 10!
one more interesting thing to note if we change da code like this:
private static int i = giveMeJ();
private static int j = 10;
private static int giveMeJ()
{
return j;
}
now the output is again 0! now am sure u know y it is so....i as it is static is initialized first by calling giveMej() in which j is not yet initialized therefore its defualt value 0 is returned and stored in i.
hope dat helps if am wrong or something missing do let me know thanks


Thanks,<br />Pallavi
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
If you add a println statement thus:

you can see the initial (defaulted) values of i and j.
[ April 11, 2004: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Yogesh Chhawasaria
Ranch Hand

Joined: Apr 02, 2004
Posts: 53

if instead of above

Why doesnt this compile. It gives me a forward reference error.
Why doesnt i is asigned 0.
pallavi utukuri
Ranch Hand

Joined: Feb 10, 2004
Posts: 182
private int i = j; //1
private int j = 10; //2
this cant compile as j is not initialized or rather not even declared at 1 its initialization and declaration takes place in line 2
instead
private int i = j; //1
private static int j = 10; //2
this will compile as line 2 executes first and the declaration & initialization takes place
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The Java Language Specification talks about this sort of thing in great detail. By the way, I don't understand all of it, but I know where to find it if I need it.
[ April 11, 2004: Message edited by: Barry Gaunt ]
 
 
subject: Forward Referencing to method