Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is called a forward reference in Java?

Tags:

java

I have been through this question on legality of forward references but not clear as to what is meant by forward references in Java language . Can someone please explain with the help of an example ?

like image 963
Geek Avatar asked Jan 24 '13 19:01

Geek


People also ask

What is forward reference in Java?

In simple terms it means referencing (accessing a variable, calling a function) that is further down in the code file. static int x=getY(); static int y=5; static int getY() { return y; } x's value is set to the result of getY() getY() is called before y's value is set to 5. x's value is therefore 0 (default integer)

What is meant by forward reference?

A forward reference occurs when a label is used as an operand, for example as a branch target, earlier in the code than the definition of the label. The assembler cannot know the address of the forward reference label until it reads the definition of the label.

Are forward references allowed in Java?

Java allows very flexible forward references. A method may refer to a variable or another method of its class, regardless of where in the current class the variable or method is defined.

What is forward reference pointer?

Forward reference is when you declare a type but do not define it. It allows you to use the type by pointer (or reference for C++) but you cannot declare a variable. This is a way to say to the compiler that something exists. Say that you have a Plop structure defined in Plop.


2 Answers

This is specifically a compilation error. And its all about ordering of class variable declarations. Let's use some code for illustrative purposes:

public class ForwardReference {        
   public ForwardReference() {
      super();
   }

   public ForwardReference echoReference() {
      return this;
   }

   public void testLegalForwardReference() {
      // Illustration: Legal
      this.x = 5;
   }

   private int x = 0;

   // Illustration: Illegal
   private ForwardReference b = a.reference();
   private ForwardReference a = new ForwardReference();
}

As you can see, Java allows you to reference a class variable in a class method, even if the declaration of the variable comes after the method. This is an example of a (legal) forward reference, and support for this is built into the Java compiler.

What you cannot do though, is declare a class variable 'a' that depends on another class variable 'b' that has not been declared yet. Dependent class variable declarations must appear in reverse order of their dependency.

On a tangent, Most, if not all IDE's will warn you if your code contains illegal reference errors.

Illegal forward references are covered in section 8.3.2.3 of the JLS.

like image 154
Perception Avatar answered Sep 22 '22 14:09

Perception


It's basically just the order that things are read by the compiler, if you have

int c = 3
int a = b;
int b = 5;

the compiler will read it from top to bottom, so it will se the first line, which declares a variable 'c', and assigns it to 3, and that is fine, then it will encounter the second line, which declares a variable 'a', and then tries to assign it to 'b'.

But now, the compiler has a problem: What is this 'b' thing? It has only yet learned about 'c', and just recently 'a', but it has no knowledge anything called 'b', since to the compiler, it has not yet been declared. So then, since the compiler can't handle all the confusion, it stops, and leaves you to figure what you have done to anger it.

So, the forward reference part would be a reference to something that does not yet exist. Forward in time perhaps..

like image 36
Tobb Avatar answered Sep 23 '22 14:09

Tobb