• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

switch & case

 
tc king
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what's the result?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you posting this as a teaser or asking what the problem is?
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Above,

The result is a compile time error.
 
tc king
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got the problem and i hope some one can tell me why?
 
tc king
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
who can tell me what's the different:
final int a=1;
and
final int a;
a=1;
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i get it!!!
thanks all of you!
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks jothi
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21427
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic