aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Final Variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Final Variable" Watch "Final Variable" New topic
Author

Final Variable

Ritu Kapoor
Ranch Hand

Joined: Oct 03, 2004
Posts: 102
Please look at the following code:

------------------------------------------
public class Class06 {
final int a;
public Class06(){
System.out.println(a); //1
a=10;
}
public static void main(String args[]){
System.out.println(new Class06().a);
}
}
---------------------------------------------
We all know that instance variables are intialized to a default value. But in the above case if at //1 I get compilation error. But if I initialize it explicitly it works fine.

My question is why variable 'i' is not being initialized a default value even though its a final variable.
sampath garapati
Ranch Hand

Joined: Sep 26, 2005
Posts: 39
I think final variables should be initialized.
Please correct me if I am wrong.

Sampath
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
Remember Restriction on final variables:They can not be reinitialized.
That is all.So if You have declared it in a method like


is right until you reinitialise it.
But

is wrong cause compiler thinks(in my opinion ,correct me if i am wrong)
that it has been given default value but still its value can be changed in
constructors i.e. reinitialisation !that is compile time error.Right?So what is the way out of it?Simple.........just initialise instance variables in EVERY constructor and don't let compiler think in other way.Now start thinking for your problem.The answer should be found out.If not,please tell.Right?
[ September 29, 2005: Message edited by: agrah upadhyay ]
Andreas Sandberg
Ranch Hand

Joined: Sep 14, 2005
Posts: 31
That's right. final instance variables must be initialized before the end of the constructor otherwise you get compilation error. Whereas 'normal' instance variables, including those with non-access modfier static, will get the default values.

Andreas


"When the compilers not happy ain't nobody happy"
Ritu Kapoor
Ranch Hand

Joined: Oct 03, 2004
Posts: 102
Thanks guys for the above explanation!
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,

Can you shed some light on this ...why the compilation error??

if the variable a has been declared as



Tx
Ritu Kapoor
Ranch Hand

Joined: Oct 03, 2004
Posts: 102
Thanks guys for the above explanation!
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579

Hi A Kumar,
your code will not compile
Correct your code(encapsulation).when corrected ,go to my previous explanation.

[ September 29, 2005: Message edited by: agrah upadhyay ]
[ September 29, 2005: Message edited by: agrah upadhyay ]
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
HI,

I asked you...

Can you shed some light on this ...why the compilation error??



Like ..

final static int a; //this gives compilation error
final static int a = 10; //this compiles fine

Its based on Ritus Code...

agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
Hey kumar Why Don't you see my first explanation.if that declaration is in class scope,it will give compile time error.
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,
This is what i can summarize...

Instance Final Variables
Instance final variables should be initialized along with their declaration..or

If they arent ...initialized..

then you can initialize...the variable either in the constructor or initializer block.. else compilation error



Final Class Variables with static modifier

Ex:static final int b;

Static final class variables should be initialized along with their declaration..or

If they arent ...initialized..

then you can initialize...the variable in static initializer block..

else compilation error


Correct me if am wrong...

Regards




[ September 29, 2005: Message edited by: A Kumar ]
[ September 29, 2005: Message edited by: A Kumar ]
sampath garapati
Ranch Hand

Joined: Sep 26, 2005
Posts: 39
please clarify me this doubt.

public class Sandys{
final int court;
public static void main(String argv[]){
Sandys s = new Sandys(99);
s.court = 88; // 1
System.out.println(s.court);
}
Sandys(int ballcount){
court=ballcount;//2
}
}

In the above if executed why it is not showing error in line //2 mentioned above even though it is final variable.

Sampath
sampath garapati
Ranch Hand

Joined: Sep 26, 2005
Posts: 39
I found the solution from the above explanation.
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
Hai Garapati,
Your Code is alright.Here you are initialising court in constructor.That is fine.see my first explanation.....i have explained in detail
sampath garapati
Ranch Hand

Joined: Sep 26, 2005
Posts: 39
yes that's what I mentioned just above your post. Thanks for the explanation to you and to Mr.A Kumar
 
 
subject: Final Variable