File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final variable declaration" Watch "final variable declaration" New topic

final variable declaration

dhana rangu
Ranch Hand

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

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

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
Dave Fishburn

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

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;
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:
subject: final variable declaration
It's not a secret anymore!