Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

initializers-referencing

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

i cant understand one of the notes about initializers that says that,

In all the initializers, forward referencing of variables is not allowed. Forward referencing of methods is allowed.


Please explain what is foward referencing and backward referencing means.

Thanks
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See Corey's SCJP Tipline - Forward Referencing.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Catherine Matthews,

In all the initializers, forward referencing of variables is not allowed. Forward referencing of methods is allowed.

Forward referencing is known as accessing a variable or method that is defined forward in the sequence. Although while initialization takes place the forward referencing of variables are not allowed but forward referening of methods is allowed.
That is true.
Instance variables are initialized in the sequence in which they appear in the code. So if you try to initialize a variable with another that is down to the prior on, compiler generates error, because upto this level the varialble may not be initialized. Instance variables are initialized from top to down or say that in order they appear in the program.
The overall fact is, you can�t use a variable until it is initialized, although there may be scenario when you are allowed to assign it a value.

int i=j;//compiler error, can�t use a field before it is initialized.
int j=5;

But if j is static, no problemo!
int i=j;//OK
static int j=5;
This is only because static variables initialization takes place when class is loaded, so there when instance variable I is initialized j already exists well initialized with value 5.

But if you are initializing a variable using a method return type, its ok.
class ForwardRef {
int k = getI();
int i=5;
public int getI() {
return i;
}

public static static void main(String[] args) {
System.out.println(�k = � + new ForwardRef().k);
}
}
This prints 0; We are simple fooled by forward referencing. You can point here the previous sequence related definitions. Variable I has not been initialized at the point it is used. So default value of k is printed.








See another scenario:


class ForwardRef {
{
i=10;
}

int i=5;

}

Why this? Simple rule; we are not using variable i but simply assigning it a value. And you know i=10 is going to be executed prior to i=5 executes because of the ordering of code they appear in the program.

One more case:

class Outerclass {

class innerclass {
int a=b;
}

int b=10;
}

Here the code compiles just fine. You recall that the non static inner class is associated with the containing class so, the b would be lot before initialized. You must have instance of the container class when you want to instantiate the inner class object so there is no problem either in using the outer class member variable or just initializing it another value.

I hope these explanation help you!
With regards,
cmbhatt
 
catherine matthews
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot yaar....
I understand it better....
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is forward referencing included in one of the SCJP 1.5 objectives?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Is forward referencing included in one of the SCJP 1.5 objectives?



Hi Megha,

There is no specifically given objective regarding forward referencing, but as I think it is like inner classes, which is also not explicitly given objective but will be stuffed in the code we will see in the exam.

So in the same way "forward referencing concept judging material" we may find stuffed somewhere in our questions.

With Regards,
cmbhatt
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!

I was playing around with your example code..The code below works...

Does it mean that the class variables get initialized to default values first before any of the methods are called. How does using i on the right hand side of the operator in getI() work far before i is declared ...

public class Main extends Thread
{

int k = getI();

public int getI() {
int t = i;
System.out.println(t) /// gives output 0
return t;
}



int i=5;

static public void main(String argv[])
{

//System.out.println(new Main().k);

}
}
[ April 01, 2007: Message edited by: megha joshi ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Megha,


How does using i on the right hand side of the operator in getI() work far before i is declared ...

public class Main extends Thread
{

int k = getI();

public int getI() {
int t = i;
System.out.println(t) /// gives output 0
return t;
}

int i=5;

static public void main(String argv[]) {

System.out.println(new Main().k);

}
}



Good!
Simple rule, Compiler is sure because you can't call instance method without
having an instance of the class. In other words you must have an instance of
the class in order to call the instance methods. Simply when you instanciate the class, then the what order I explained earlier and you better know too is followed. It is true that at the time when you call getI() i is not initialized but it is declared, that is why you didn't get any error. But until the constructor of the super class is completed, i of our class is not initialized (simple rule of Java from top to bottom initialization).

Got it Megha?

Thanks and Regards,
cmbhatt
[ April 01, 2007: Message edited by: Chandra Bhatt ]
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! got it.

Our Java compiler is wonderful
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Megha,

Don't you feel irritated of this Compiler. It makes us happy and irritated too. It is not like us, it is bound to millions of strict rules. But still it is wonderful.



Thanks and Regards,
cmbhatt
 
this llama doesn't want your drama, he just wants this tiny ad for his mama
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!