This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Forward referencing

 
Shafeeq Sheikh
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anybody???
 
ricky gonzalez
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody help me out here???
Thanks....
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
find a same discussion at http://www.javaranch.com/ubb/Forum24/HTML/001464.html
Regds
Rahul
 
Shafeeq Sheikh
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rahul...... That helped a lot
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats relly a nice explanation. Great Nelakonda

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic