File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes return triggers method end but overriden? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "return triggers method end but overriden?" Watch "return triggers method end but overriden?" New topic
Author

return triggers method end but overriden?

Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
Hi there, in the below code is a question from Marcus Green Q's on Examulator..
public class Phr4jr{
private Phr4jr(){
System.out.print(1);
}
public static void main(String argv[]){
Phr4jr ph = new Phr4jr();
System.out.print(ph.go());
}
public int go(){
try{
System.out.print(2);
return 1;
}finally{
System.out.println(3);
return 2;
}
}
}
What is the output?
Well, i guessed it right and chose 1232 ... guessed meaning, i do not really understand why it isn't 1231 - the issue being the last number that is printed, is the number that is returned, and the first return statement's arg is 1...the return statement says "End of method - go back!", to which the method says "Oh yeah? well first let me do 'Finally!'" and then finally has a return in it which gives 2 as an arg.
so 2 is the last number, is that why?
thanks in advance


giddee up
Dan Culache
Ranch Hand

Joined: Jan 24, 2003
Posts: 70
Hi Jasper,
I guess you got it right. To make the case stronger I tried :
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
Hi Dan, the code gives a compiler error of 'unreachable statement' for the 3rd return statement, but i get the general idea
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: return triggers method end but overriden?