| Author |
return in Switch case
|
pradeepta chopra
Ranch Hand
Joined: Jul 05, 2008
Posts: 137
|
|
When i write a return statement in the Switch case,where does the control returns to?? the line 1 is not printed
|
 |
Steven Landers
Ranch Hand
Joined: Nov 02, 2008
Posts: 30
|
|
The "return" keyword always has to do with exiting the current method (with a few peculiarities regarding the finally{} block). "break" will exit a switch statement. In this case, "return" returns the flow to the "caller of main" which in this case, would be the JVM, I imagine. You can test this similarly by creating another function, with a return type of "void" - with this same switch statement. If you call this new function from main(), you'll note that the return statement "returns" the flow to main - and your //line 1 is never printed.
|
 |
pradeepta chopra
Ranch Hand
Joined: Jul 05, 2008
Posts: 137
|
|
Thanks Steven Landers,ur suggestion works
|
 |
Ganeshkumar cheekati
Ranch Hand
Joined: Oct 13, 2008
Posts: 362
|
|
hi pradeepta did you run the code? i am getting java.lang.NoClassDefFoundError at runtime....
|
SCJP5 and SCWCD1.5
Think Twice Act Wise...
|
 |
pradeepta chopra
Ranch Hand
Joined: Jul 05, 2008
Posts: 137
|
|
Ofcourse i have run the code. but i dont get any Exception,check for the directory and its path where you are storing the file
|
 |
Ganeshkumar cheekati
Ranch Hand
Joined: Oct 13, 2008
Posts: 362
|
|
HI pradeepta......... class k{ public static int main(String[] args) { int x=1; switch(x+2){ case 4: System.out.println("4"); break; default: System.out.println("default"); break; case 3: System.out.println("3"); return ; //break; } System.out.println("returned"); //line 1 } } i have compiled the same code which is posted by you with 1.5compiler.i am getting compilation error as: missing return value return ; ^
|
 |
pradeepta chopra
Ranch Hand
Joined: Jul 05, 2008
Posts: 137
|
|
hi GaneshKumar, you probably have done a typing mistake. make sure that it is return; //no space between return and;
|
 |
Ganeshkumar cheekati
Ranch Hand
Joined: Oct 13, 2008
Posts: 362
|
|
return; still it is giving the same compilation error missing return value; return; ^
|
 |
pradeepta chopra
Ranch Hand
Joined: Jul 05, 2008
Posts: 137
|
|
thats because you have declared public static int main() and that requires you to return a value
|
 |
Ganeshkumar cheekati
Ranch Hand
Joined: Oct 13, 2008
Posts: 362
|
|
actually i tried your code by returning some int value so that i put int as a return type.... ok i have changed it to void and compiled but now it is compiled and throwing runtime exception as NoClassDefFoundError  [ November 05, 2008: Message edited by: Ganeshkumar cheekati ]
|
 |
pradeepta chopra
Ranch Hand
Joined: Jul 05, 2008
Posts: 137
|
|
|
i have compiled your post's code with return; and void modifications ,it compiles fine and no Exception is thrown.
|
 |
Ganeshkumar cheekati
Ranch Hand
Joined: Oct 13, 2008
Posts: 362
|
|
which compiler you are using? what is the output?
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
ganesh just do what pradeepta is saying. Change the return type of main to void...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Ganeshkumar cheekati
Ranch Hand
Joined: Oct 13, 2008
Posts: 362
|
|
yeah i have done that. i am getting NoClassDefFoundError. but she got output for the same code. i dont know why i am getting this exception? [ November 05, 2008: Message edited by: Ganeshkumar cheekati ]
|
 |
Amit Ghorpade
Bartender
Joined: Jun 06, 2007
Posts: 2552
|
|
Originally posted by Ganeshkumar cheekati: yeah i have done that. i am getting NoClassDefFoundError. but she got output for the same code. i dont know why i am getting this exception?
Firstly NoClassDefFoundError is not an exception. Where is the compiled .class file stored on your disk? are you executing java k.class from that same location?
|
SCJP, SCWCD.
|Asking Good Questions|
|
 |
Ganeshkumar cheekati
Ranch Hand
Joined: Oct 13, 2008
Posts: 362
|
|
yeah i got it now. output is 3. thanks to every one who has spended their time for me...  [ November 06, 2008: Message edited by: Ganeshkumar cheekati ]
|
 |
 |
|
|
subject: return in Switch case
|
|
|