• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

return; not allowed in initializers

 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I am confused. This static initializer has a return.

So why can't initializers have return statements?
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marlene, static initializers are not placed in the constructor body.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic