• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

return; not allowed in initializers

 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marlene, static initializers are not placed in the constructor body.
 
I'm sure glad that he's gone. Now I can read this tiny ad in peace!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic