Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Return Statement..

 
Murgan Sub
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are return statements not allowed in
static initializer or an instance initializer?
Kindly Explain..
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • 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.
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • 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 ]
 
Jian Yi
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • 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
 
Jennifer Wallace
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • 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 ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Corey & Jennifer,
Both of your replies are greatly appreciated.
-Jenny
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic