Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Switch Case Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Switch Case" Watch "Switch Case" New topic
Author

Switch Case

Liang Anmian
Ranch Hand

Joined: Jun 25, 2004
Posts: 119
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"?


Current Status:<br /> <br />SCJP 1.4<br />SCJD (in progress)
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
"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.


Mike Gershman
SCJP 1.4, SCWCD in process
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
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


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Nicholas is correct, but this doesn't compile either:
shri mon
Ranch Hand

Joined: Jan 11, 2005
Posts: 63
i think final variables must be initialised at the time of declaration or in the constructors.
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
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

Joined: Mar 13, 2004
Posts: 1272
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.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830


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

Joined: Jun 25, 2004
Posts: 119
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

Joined: Jul 19, 2004
Posts: 298
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

Joined: Mar 13, 2004
Posts: 1272
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Switch Case
 
Similar Threads
Assertion Question
j++ IN switch()
a switch problem
switch case and wrapper objects
which break statement doulh I use