posted 17 years ago
Neha,
1)
When you invoke (new AQuestion()).i in your main() method, the result is the value of that 'i' gets initialized with inside the constructor, whose body looks like this:
public AQuestion()
{
i = giveMeJ();
j = 10;
}
So, when 'i' tries to get the value from giveMeJ(), 'j' shouldn't have been initialized to 10, rather it would have the 0, the default value for an 'int'. So, i = 0 and the same would be printed.
One Important Point is: The instance variables get initialized in the order
they're read. So, if it were:
private int j = 10;
private int i = giveMeJ();
Then, you would get output as 10.
2) Forward referencing occurs because you're trying to assign a variable to value of some other variable that doesn't exist at that point at all!
Cheers!
Sanjeev