File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt regarding Constructors - - JLS 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 "Doubt regarding Constructors - - JLS" Watch "Doubt regarding Constructors - - JLS" New topic
Author

Doubt regarding Constructors - - JLS

Neelkanth K
Greenhorn

Joined: Feb 13, 2001
Posts: 4
Hi
Can somebody throw more light on this,
This is from JLS(page 178)
________________________________________________________________
Except for the possibility of explicit constructor invocations, the body of a constructor is like the body of a method (�8.4.5). A return statement (�14.15) may be used in the body of a constructor if it does not include an expression.
_________________________________________________________________
As far i know, constructors cannot return a value.
Please help, i am very cconfused.
Thanx
Neelkanth
Richard Agnes
Greenhorn

Joined: Feb 12, 2001
Posts: 22
that's interesting, I wasn't aware of that, but it seems to work take a look output is 100:
public class Tester{
int m=100;
boolean b=true;

Tester(){
if(b){
return;
}
m+=100;
}


public static void main(String args[]){
Tester t=new Tester();
t.showM();
}

void showM(){
System.out.println(m);
}

}//end class
I guess it's to halt processing in the constructor.
Wasim Ahmed
Ranch Hand

Joined: Jan 31, 2001
Posts: 90
JLS is correct you can have empty "return" keyword in the constructor but you cannot assign any value to that return. On the other hand if you add return type in the constructor signature, that will turn into a method with the same name as class. (another strange act of JAVA)see the following example.
example:
class Aclass
{
Aclass()
{System.out.println("I am in Constructor");
return;
}
int Aclass()
{
System.out.println("I am in method");
return 4;
}
public static void main( String argv[] ){
Aclass a = new Aclass();
a.Aclass();
}
}
Output : I am in Constructor
I am in method
Neelkanth K
Greenhorn

Joined: Feb 13, 2001
Posts: 4
Thanks Wasim & Richard,
Now i got it,
Thanks for explanation
Neelkanth
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Your name 'Neelkanth K' does not comply with the JavaRanch naming policy. Please choose one that meets the requirements.
Thanks!
Ajith

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt regarding Constructors - - JLS
 
Similar Threads
JLS - Cosntructor
return statement from Constructor
Constructor doubts
Question on constructors
Let's get this straight!!!