aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about forward refrencing 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 "about forward refrencing" Watch "about forward refrencing" New topic
Author

about forward refrencing

geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208
I got it from
http://www.angelfire.com/or/abhilash/Main.html

public class AQuestion
{
private int i = j;
private int j = 10;
public static void main(String args[])
{
System.out.println((new AQuestion()).i);
}
}

Answers

1.Compiler error complaining about access restriction of private variables of AQuestion.
2.Compiler error complaining about forward referencing.
3.No error - The output is 0;
4.No error - The output is 10;

Answer is 2.

From this i interpreted about Forward Refrencing is that if the assigning value is not available at compile time then it gives the error as above.


But please look at the below 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);
}
}

Answers


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;

Answer is 0

Here why there is no compiler error? And kindly explain me about Forward Refencing(whether what i have interpreted is correct or not.

Thanks,
Geeta V
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Well forward referencing inside methods is allowed. If you call a method which uses a field which is uninitialized. Actually the initializations actually are transferred to the constructor. So the code would become



So if you call a method and the method access j, then the value of j would be 0 at the time of method call.



And if you assign a value to j before the method call, then that value of j will be used



So basically j actually has a value when i is initialized. But still the compiler doesn't allow you to access it's value directly. I don't know the reason about though. The reason might be in the JLS...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

This may help you. web page


My Blog SCJP 5 SCWCD 5
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
The main idea behind forward referencing is the order of execution.
The order of execution of static variable initialization, static block initialization, variable initialization, initialization block, constructor plays important role in forward referencing.

You should have idea what variables are loaded when class is loaded, what are loaded when class is run, you will get the whole idea behind forward referencing.


Here order of execution says that i should be loaded before j, that's why it is illegal to reference j.



Here i is not loaded at compile time, its value depends on runtime, but j is compile time, so j is available when giveMeJ() is executed by the jvm.


SCJP 6
 
Consider Paul's rocket mass heater.
 
subject: about forward refrencing