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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Forward referencing" Watch "Forward referencing" New topic
Author

Forward referencing

Shafeeq Sheikh
Ranch Hand

Joined: May 12, 2000
Posts: 68
Found this on a mock exam and the answer completely stumped me!!!
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);
}
}
1. Compiler error complaining about access restriction of private variables of AQuestion.
2. Compiler error complaining about forward referencing.
3. No Compilation error - The output is 0;
4. No Compilation error - The output is 10;
I thought the answer would be 2. but it is given as 3. Compiled and executed the code and the answer given in the exam is correct.....
Just can't figure out how..... any inputs???
Thanks.....
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Shafeeq Sheikh:
Found this on a mock exam and the answer completely stumped me!!!
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);
}
}
1. Compiler error complaining about access restriction of private variables of AQuestion.
2. Compiler error complaining about forward referencing.
3. No Compilation error - The output is 0;
4. No Compilation error - The output is 10;
I thought the answer would be 2. but it is given as 3. Compiled and executed the code and the answer given in the exam is correct.....
Just can't figure out how..... any inputs???
Thanks.....


Hi,
The code will compile because instead of assigning the
value directly,u are passing the return value of the called function.
Its like:
public class AQuestion
{
private int i;//declaring the variable

private int j = 10;
i=giveMeJ(); // assinging the value by invoking the function.
private int giveMeJ()
{
return j;
}
public static void main(String args[])
{
System.out.println((new AQuestion()).i);
}
}
But, i thought the output would be 4 instead.

Shafeeq Sheikh
Ranch Hand

Joined: May 12, 2000
Posts: 68
anybody???
ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
I don't know either, but from a reverse engineering point of view, obviously i was still the default class value, and nothing was assigned to it. So, i = giveMeJ(), even though syntactically correct, assigned nothing to i. I am just guessing.
Shafeeq Sheikh
Ranch Hand

Joined: May 12, 2000
Posts: 68
Can anybody help me out here???
Thanks....
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
find a same discussion at http://www.javaranch.com/ubb/Forum24/HTML/001464.html
Regds
Rahul
Shafeeq Sheikh
Ranch Hand

Joined: May 12, 2000
Posts: 68
Thanks Rahul...... That helped a lot
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Friends,
I have an idea about this question.Please be patient and read the answer.
The answer is definitely 3.
As one of our friends specified we are declaring and initializing the private instance variable at the same line.
private int i = giveMeJ();
which is equilant to
private int i;
i = giveMeJ();
Even though the instance variable i is declared private ,we can access it in the main method of the same class.But if we copy and paste the same main method in another class (which may extend the AQuestion class or any other class) we will get a compile error.We cannot access private variables outside the class in which it is declared.
And coming to the output.
It is 0 instead of 10 as we expect .
Because by the time the line
private int i = giveMeJ(); is executed j is not initialized to 10.So the method giveMeJ returns 0 which is default value for an int.
If we just interchange the two lines we will get 10 as output.
private int j = 10;
private int i = giveMeJ();
Here j is initialised before the giveMej() is executed .So we get 10 as output.
This is what I think about the question.If u have any new ideas please let me know.
My mail id is muraleedharn@hotmail.com
Thanking you,
Muraleedhar Nelakonda
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thats relly a nice explanation. Great Nelakonda

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Forward referencing