Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

final variable declaration  RSS feed

 
dhana rangu
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
This might be a simple question.But i didn't get it.Plz help

At exp1 ie a=a+1 ..we get an error " cannot assign value to final variable..." Thats fine since a is final variable.
But in exp2 ie b=b+1 we get the error " variable b might have already assigned a value".
What i doubt is " what is the difference between the declaration of variables a and b.Can't we treat b as final variable.If so why are we getting different error messages.What happens if we donot initialize b in its declaration stmt..." plz help

dhana
 
Michael Sullivan
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the difference is in how you have initialized them.
Here: final int a=12; you declare and initialize all on one line, which is all fine. But here: final int b; you have just declared the final variable without initializing it. Then on the next line: b=13; you initialize it, which is still good since you haven't previously assigned it a value, and haven't tried to use it.
Once you initialize the Final variable, thats it... your done.
So thats why when you try to do b=b+1; you get an error. You have already initialized your final variable, and may not do so again.
[ December 26, 2003: Message edited by: Michael Sullivan ]
 
dhana rangu
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yah michael

what u said is correct.we know that in switch - case
the data type of case must be final int .Its like

even though b is a final variable we cannot use b in case stmt 2.
Why this is so...??Can you plz explain

dhana
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can only use compile-time constants or literals for the case clause (of course, they must be implicitly int convertable). If you declare and initialize a final variable in one line, it's considered a compile-time constant. Otherwise, it's not.
 
dhana rangu
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi vad
thanx a lot for ur answer
dhana
 
Dave Fishburn
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just as a point of clarification.
If you remove the extra assignment to b, wouldnt the compiler then complain about b not being initialized?
 
raghuram gannavarapu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi vad,
i have a doubt regarding the code sent by Mr Dhanu.
class test{
public static void main(String args[]){
final int a=12;
final int b;
b=13;
a=a+1;//exp 1
b=b+1;//exp 2;
}
}
i can't understand why both the errors in the code doesnot appear at the same time.
can you please explain why and when it happens like this?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!