• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Switch Case

 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code does not compile:



My question is why? Doesn't integer values have a default value of 0? So I thought "case j" will automatically mean "case 0"?
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"final int j;" is called a blank final and j is not a constant expression.

A case requires a constant expression that can be evaluated at compile time. "final int j;" promises to provide a value later. When you don't supply one in an initializer expression, javac assumes you will supply one later, before j is used to supply a value. That leaves the value of j undetermined at compile time.
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All local variables will not be given default values, thus, no matter j is final or not, it is a local variable, which means it does not have default value, and you need to set for it explicitly.

Only class-level and instance-level variables will be given default values.

Nick
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nicholas is correct, but this doesn't compile either:
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think final variables must be initialised at the time of declaration or in the constructors.
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Final instance variables must be initialized in the constructor or during its declaration itself else it throws a compile time error.
For static final variables , the initialization should be done during declaration
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

For static final variables , the initialization should be done during declaration


You can also use a static initializer to initialize a static blank final.

However, that will not create a "constant expression that can be evaluated at compile time" for use in a case label.
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I got this point that , final variable must have a value ( must initialize ) before completing constructor . So this class have a default constructor like this :

So compiler will check , if this constructor initializing all the final variables or not ... so thatswhy in this case compiler giving error because compiler is not initialising final variable j .



You can also use a static initializer to initialize a static blank final.


It is clear because static blocks are execute at the time of class loading , so final variable will have a value before completing constructor .



However, that will not create a "constant expression that can be evaluated at compile time" for use in a case label.



please any body can explain me this line . why so ?

thanks a lot .
[ January 14, 2005: Message edited by: rathi ji ]
 
Liang Anmian
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nicholas Cheung:
All local variables will not be given default values.......



Oh silly me! I've forgotten about this rule. Thank you very much for your reminder.
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

However, that will not create a "constant expression that can be evaluated at compile time" for use in a case label.



Even i am confused with this
Why is the below program giving compiler error at
case i: saying case expressions should be constant expressions
even though i is declared to be final

public class CaseClass {
static final int i;
static {
i = 10;
}
public static void main(String[] args) {
switch(10) {
case i: {
System.out.println("hii");
}
}
}
}

Please someone explain this phenomenon??
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler will look no further then the initializer that is part of the final variable's declaration to discover the variable's value. If there is no initializer, or if the initializer refers to other variables that are not themselves constant expressions, then the variable is not considered a constant expression by the compiler.

In the sample code, "static final int i;" does not give "i" a value. Therefore, i is not a constant expression. i is a constant, since i is final, but the constant value of i will not be known until the static initialization block is actually executed.

Since i is not a commpile-time constant expression, i cannot be used in a case clause.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic