aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt on Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt on "Vairable declaration & forward reference"" Watch "Doubt on "Vairable declaration & forward reference"" New topic
Author

Doubt on "Vairable declaration & forward reference"

Mani vannan
Ranch Hand

Joined: Aug 21, 2004
Posts: 185
Hi,
I have come across the following questions in some SCJP mock. (Hope, you dont feel bore for reading the code.)

public class AQuestion
{
private int i = j;
private int j = 10;
public static void main(String args[])
{
System.out.println((new AQuestion()).i);
}
}
I know that the above code is error reported by the compiler, since forward reference problem with the variable j.

Look at this code,

public class AQuestion
{
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);
}
}

Here also, forward reference problem, But, how come the it gets compiled without any error & outputs 0? Please clarify this.


Manivannan
Jas Oberai
Ranch Hand

Joined: Jan 16, 2005
Posts: 231
Hi Manivaanan,
This question has been discussed a lot of times in the past..while i was preparing for the exam..this link provided me the best explanation on forward referencing.I'm sure...after you go through this link you will have no problems understanding these forward refencing scenario's.

http://radio.javaranch.com/corey/2004/05/13/1084483439000.html

Thanks


SCJP 1.4 (88%)<br />SCWCD 1.4 (88%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt on "Vairable declaration & forward reference"
 
Similar Threads
Variable initialization
Forward Referencing
forward refrerences
please explain why the compiler is not complaining and why the output is 0
Forward reference