aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Forward references to variables?? 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 "Forward references to variables??" Watch "Forward references to variables??" New topic
Author

Forward references to variables??

venkatesh badrinathan
Ranch Hand

Joined: Aug 03, 2008
Posts: 77
"Forward references to variables gives a compiler error"
can anybody explain me the above statement please...


SCJP1.5
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

It means that if you try to access the value of a field before its declaration in the class. eg


What you can do is that you can assign a value to a field before it is declared as-

But what would happen in a case where you call a method to assign a value to a field and that method uses an undeclared field. Eg-


In that case 0 will be assigned to a. This is because after compilation all the intialization statements are transferred to the constructor in the order of their declaration and the fields are assigned default values also.
See this example

You must be thinking how did a get value 10. This happens because after compilation the code would look as-

So when getVal() is called b has the value 10....


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
venkatesh badrinathan
Ranch Hand

Joined: Aug 03, 2008
Posts: 77
Thanks AnkitJi Garg, you have taken explained me well and i have understood most of the concepts. but i still have some issues here..
1.i could compile the following code but gives an exception at runtime.
class A
{
{
b = 10;
}
int getVal()
{
return b;
}
int a = getVal(); //10
int b;
A()
{
System.out.println("hello");
}
}
2. When i remove the {} brackets in the followin snippet it gives compiler err.. but i donno what those empty {} does??? can you please explain...
{
b = 10;
}
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1



This code in a class known as Initializer block !

which initialize the instance variable when class gets loaded into memory and before constructor gets called !


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

First of all the code doesn't give any exception at runtime. I think you did some mistake in writing the main method. This is new code. try this-


and about

This is an intializer block. It is executed when an object of a class is intialized. It's sole purpose is to intialize instance fields. And by the way even if you remove the b = 10; statement, then also it will compile. try this-
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Originally posted by venkatesh badrinathan:
Thanks AnkitJi Garg, you have taken explained me well and i have understood most of the concepts. but i still have some issues here..
1.i could compile the following code but gives an exception at runtime.

Which exception , and you cant run this code as it dose not have main method !

Originally posted by venkatesh badrinathan:
[QB
2. When i remove the {} brackets in the followin snippet it gives compiler err.. [/QB]


Its not giving me any error , when I remove those intilizer block !
venkatesh badrinathan
Ranch Hand

Joined: Aug 03, 2008
Posts: 77
i did not remove the initializer block as a whole.. but only the curly brackets.. as..
class A
{
b = 10;

int getVal()
{
return b;
}
int a = getVal(); //10
int b;
A()
{
System.out.println("a = "+a);
}
public static void main(String[] args)
{
new A();
}
}

am much confused thinking of the difference when the {} were removed and when it is not....
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

When you removed curly braces you are telling compiler a very strange thing, he does not understand what basically you want to do , If its variable then there is no data type associated with it . If its a intilizer block then no braces !!

The braces plays important role in telling compiler that this block gets executed before constructor call !

am much confused thinking of the difference when the {} were removed and when it is not


There is no difference , because this two thing cant be compared with each other . One is proper syntax in defining 'Inti Block' and other is a semicolon syntax less statement , which compiler wont entertain !
venkatesh badrinathan
Ranch Hand

Joined: Aug 03, 2008
Posts: 77
Thank you sagar for your reply..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Forward references to variables??