Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Return Statement.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Return Statement.." Watch "Return Statement.." New topic
Author

Return Statement..

Murgan Sub
Greenhorn

Joined: Feb 06, 2002
Posts: 22
Why are return statements not allowed in
static initializer or an instance initializer?
Kindly Explain..
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
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 ]

<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
Jian Yi
Ranch Hand

Joined: Feb 01, 2002
Posts: 127
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

Joined: Nov 30, 2001
Posts: 102
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

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Jian Yi
Ranch Hand

Joined: Feb 01, 2002
Posts: 127
Corey & Jennifer,
Both of your replies are greatly appreciated.
-Jenny
 
GeeCON Prague 2014
 
subject: Return Statement..