aspose file tools*
The moose likes Beginning Java and the fly likes switch case with final Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "switch case with final " Watch "switch case with final " New topic
Author

switch case with final

kalpana Kumar
Ranch Hand

Joined: Jul 03, 2003
Posts: 65
Hi all,
In the following code
public static void main(String args[]){
final int a=1;
final int b;
b=20;//is this b not final
int x=0;
switch(x){
case a:
case b: //compiler error


}
}
I want clarification as to why is b not considered to be a compile time constant???.

Thanks
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10
It's not a constant because it can be changed. The compiler does not know whether or not you will change it, but it knows that it might be changed because it is not final.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
kalpana Kumar
Ranch Hand

Joined: Jul 03, 2003
Posts: 65
Marilyn,
Thanks a lot for the response.
But in the above code b is defined to be final. it is initialised in the next line.
I wanted to know if there is any difference in this type of initialization.

Thanks
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by kalpana Kumar:
final int a=1;
final int b;
b=20;//is this b not final


Both a and b are final, but a is declared with an initializer
that is a constant expression. b is declared as blank final.

Originally posted by kalpana Kumar:
But in the above code b is defined to be final. it is initialised in the next line.
I wanted to know if there is any difference in this type of initialization.


Yes there is. Here are two pointers into the JLS you can
take a look at:

constant expressions (see the last two items in the bulleted list)

definite assignment of blank finals
[ November 06, 2005: Message edited by: Brian Cole ]

bitguru blog
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
You have confused a constant with a final.
A final is not always a constant, but a constant is always final.
Your issue is related to constants, not finals.
This is why you observe some finals, that are not constants, behaving differently to other finals, that are constants.

The links above are your point of call (JLS 15.28 and JLS 16).
Here is some fun: http://jqa.tmorris.net/trivia/GetQAndA.action?qids=13


Tony Morris
Java Q&A (FAQ, Trivia)
kalpana Kumar
Ranch Hand

Joined: Jul 03, 2003
Posts: 65
Thanks a lot for all the replies.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: switch case with final
 
Similar Threads
I dont understand switch case statement
what is compile time constant in switch case?
Swtich Statement
Compile Time constant??
Legal expressions for switch and case statements