aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "switch & case" Watch "switch & case" New topic
Author

switch & case

tc king
Greenhorn

Joined: Nov 10, 2006
Posts: 11
what's the result?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Are you posting this as a teaser or asking what the problem is?
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Hi Above,

The result is a compile time error.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
tc king
Greenhorn

Joined: Nov 10, 2006
Posts: 11
i got the problem and i hope some one can tell me why?
tc king
Greenhorn

Joined: Nov 10, 2006
Posts: 11
who can tell me what's the different:
final int a=1;
and
final int a;
a=1;
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Hi Above,

final int a=1; //this is a compile time constant.

final int a; //this is a run time constant.
a=1;

For the case statements in switch, it should be a compile time constant.
tc king
Greenhorn

Joined: Nov 10, 2006
Posts: 11
i get it!!!
thanks all of you!
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Jothi,
Can you summarize the initialization of instance varibales,static variables, methods,static methods,staic block,initializaion block etc at the runtime and compile time.
Though I have gone through various threads on these topics but unfortunately have not summarised the all things at one place.


~Sanjeev Singh<br />SCJP 1.5
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Hi Sanjeev,

Here you go, I'm explaining it with an example and I have numbered the lines in the order in which they get executed.

public class Main {
static int s3 = 10; // First
static{ // Second
int s1 = 1;
}
static{ // Third
int s2 = 2;
}
static void myMethod(){ // Fourth
System.out.println("Im from the static method");
}
int k = 5; // Sixth
{
System.out.println("Normal initializer block"); // Seventh
}
public static void main (String args[]) { // Fifth

}
}

Hope this helps. Also note that the Sixth and Seventh comes to play or in other words gets initialized only when you create an object using the new keyword.

Also note that the vaiables declared inside a static block are not accessible from the main method as their scope is limited to only those static blocks(I have declared them inside the static block).

Hope you understood the concept.
[ November 11, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
raja kanak
Ranch Hand

Joined: Oct 18, 2006
Posts: 135
thanks jothi


live
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Thanks Jothi,
Preety good and nice explanation ,
Still a question,which one will be (loaded and/or initilaized) first(if it is violating the order of appearance)?
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Hi Sanjeev,

The class gets loaded by the JVM during run time. If there are static blocks and static methods, then static blockes are initialized first followed by static methods. Even if you put your static blocks at the end, first it is taken care before the JVM can look for static methods. Now for the code that you have posted, I'm not understanding as it won't make it past the compiler when you just say
final static int b;
You must have given it a value somewhere before you can use it in your program. Think about it???
tc king
Greenhorn

Joined: Nov 10, 2006
Posts: 11
thanks!
there is another problem about static and final:

this got a compile error if change line 3 to final int z; or static int z; it can work,why?
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Hi Above,

Please put the whole code, otherwise it would be difficult to give any suggestions. From the above code, I can simply say that it won't run because there's no main method. But I guess you want to know something other than that. So please put the entire code that you tried compiling.

Thanks.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

this got a compile error if change line 3 to final int z; or static int z; it can work,why?


If you change line 3 to "final int z", you are changing it to an instance variable, which can be assigned (once) in the constructor.

If you change line 3 to "static int z", it is still a static variable, but since it is no longer final, it can be assigned again -- including by the constructor.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: switch & case