Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Final Variable

 
Ritu Kapoor
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think final variables should be initialized.
Please correct me if I am wrong.

Sampath
 
agrah upadhyay
Ranch Hand
Posts: 579
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ritu Kapoor
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for the above explanation!
 
A Kumar
Ranch Hand
Posts: 980
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for the above explanation!
 
agrah upadhyay
Ranch Hand
Posts: 579
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 980
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 579
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found the solution from the above explanation.
 
agrah upadhyay
Ranch Hand
Posts: 579
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes that's what I mentioned just above your post. Thanks for the explanation to you and to Mr.A Kumar
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic