File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about forward refrencing

 
geeta vemula
Ranch Hand
Posts: 208
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9496
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may help you. web page
 
Punit Singh
Ranch Hand
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic