This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes return; not allowed in initializers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "return; not allowed in initializers" Watch "return; not allowed in initializers" New topic
Author

return; not allowed in initializers

Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I just figured out something I think is kind of interesting (and the more I understand, the less I have to remember).
I was wondering why the return statement is allowed in a constructor but not in an instance initializer.
In the following example, the compiler inserts the initializer code into the constructor code after the call to the superclass constructor.
If there were a return in the initializer code, you would return from the constructor after executing the initializer code but before executing the constructor code.

Now I won�t have to remember that returns are not allowed in initializers. And I won�t have to remember that initializers are executed after superclass constructor invocations.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
There�s more to the story...
The return statement discards the values on the operand stack of the current frame and reinstates of the frame of the caller of the method or constructor. There is no frame associated with the initializer code.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
Marlene -
You said:
"The more I understand, the less I have to remember"
Can I quote you on that? It is marvelous, and it absolutely captures the spirit of our book!
-Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Now I am confused. This static initializer has a return.

So why can't initializers have return statements?
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
Ah Marlene -
You're too fast, i was going to edit my last post...
Anyway, just FYI, this research that you're doing is very interesting, AND at this point in time the exam will not cover this topic. So it's good from a general knowledge point of view, but if your focus is the exam, then your time would be better spent on other topics.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
>>if your focus is the exam, then your time would be better spent on other topics
Yes. Back to statements and flow control.
>>Can I quote you on that?
It�s fine with me.
(This winter I was a technical reviewer for a college textbook on OO programming with Java. I think the other reviewers were college professors. Not me. So the publisher asked us if they could quote us. My reply was: I never say anything worth quoting.)
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Marlene, static initializers are not placed in the constructor body.


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: return; not allowed in initializers