Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final variable declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final variable declaration" Watch "final variable declaration" New topic
Author

final variable declaration

dhana rangu
Ranch Hand

Joined: Oct 01, 2003
Posts: 65
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

Joined: Dec 26, 2003
Posts: 235
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

Joined: Oct 01, 2003
Posts: 65
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

Joined: Aug 25, 2003
Posts: 504
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

Joined: Oct 01, 2003
Posts: 65
hi vad
thanx a lot for ur answer
dhana
Dave Fishburn
Greenhorn

Joined: Dec 18, 2003
Posts: 3
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

Joined: Dec 19, 2003
Posts: 3
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final variable declaration
 
Similar Threads
Compile time constant ?
final variable in a for loop
Operator self test question and Flow Control doubt.
Why final constants won't take the default values?
a query about switch