GeeCON Prague 2014*
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


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%)
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30596
    
154

The article is now at http://www.coderanch.com/how-to/java/ForwardReferencing


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Doubt on "Vairable declaration & forward reference"