• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Return Statement..

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are return statements not allowed in
static initializer or an instance initializer?
Kindly Explain..
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From JLS 8.6 Instance initializer

It is a compile-time error if an instance initializer cannot complete normally (�14.20). If a return statement (�14.16) appears anywhere within an instance initializer, then a compile-time error occurs.


From JLS 8.7 Static initializers

If a return statement (�14.16) appears anywhere within a static initializer, then a compile-time error occurs.

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Murgan, you have to realize that static initializers are executed once when the class is loaded by the JVM. If you think about it, it doesn't make sense to return from this process, if you could get away with this then one could abort the process of any class getting loaded and thus no one could create object instances of this class. (Just one possible interpretation)
[ February 16, 2002: Message edited by: Rajinder Yadav ]
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Murgan Sub:
Why are return statements not allowed in
static initializer or an instance initializer?


Can anyone kindly tell me what an initializaer is? Can anyone give an example?
Thanks,
Jenny
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Initializer is a piece of code used for initialising purposes. There are two kinds
1>static initializers
static{
//u can code here;
}
This is executed only once - when the first object of the class is created OR the first time when any static member of the class is accessed.

2>instance initializer
{
//u can code here;
}
This piece of code is executed when an instance of the class is created.
Also, see http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#246032
[ February 20, 2002: Message edited by: Jennifer Wallace ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jenny Yin:

Can anyone kindly tell me what an initializaer is? Can anyone give an example?
Thanks,
Jenny


Here's one more example for you in the context of a full class:

I was in a hurry, so I didn't test this, but you should see the output given above.
Instance initializers aren't really very different from a constructor. The main use for them is to either combine common code that would be used in multiple constructors or to perform initialization for an anonymous class, which, as it has no name, has no constructor.
I hope that helps,
Corey
 
Jian Yi
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Corey & Jennifer,
Both of your replies are greatly appreciated.
-Jenny
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic