wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes constructor with return value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "constructor with return value" Watch "constructor with return value" New topic
Author

constructor with return value

sanjey asok
Ranch Hand

Joined: Jul 11, 2009
Posts: 39
Class A{

Public int A(){
Return 0;
}
}

As I know ,constructor should not have a return value but if I try the above code in eclipse it does not throw any error ,
A a=new ();

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

hope you mean return not Return. java is case sensitive language.

and your example dont show a constructor instead it has a method . remember
1.*constructor dont have a return type*
2. method can have a name as constructor name.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

That's not a constructor, that's a regular method that happens to have the same name as the class.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
rahul S Sharma
Greenhorn

Joined: Jun 08, 2011
Posts: 21
No Way...
The default return type of a constructor is the object of the same class. You cannot make a constructor explicitly return any other data type.

class Test
{
Test()
{
return ;
}
}
In that case, compiler will not show any error....
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

sanjey asok wrote:Class A{

Public int A(){
Return 0;
}
}

As I know ,constructor should not have a return value but if I try the above code in eclipse it does not throw any error ,
A a=new ();




your understanding of constructor is little wrong.as above said,it is a regular method.
But yes you are right i have read somewhere that constructor return some value to an operating system but that is of no use for us.So in java terms there is no value return by constructor.


Read this to understand more about constructor.


SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

"sanjey asok" & "rahul S Sharma"

Welcome to JavaRanch,

As this code is very small and easily understandable,so please use code tags while posting larger codes.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

rahul S Sharma wrote:The default return type of a constructor is the object of the same class. You cannot make a constructor explicitly return any other data type.

That's not really true. Constructors do not return anything, and constructors don't have a return type.

A constructor is a special block of code that is called to initialize a new object. The constructor is not what creates the object - it just initializes the object, the new operator is what actually creates the object and calls the constructor to initialize it.

The thing that makes this confusing is that it is possible to have regular methods that have the same name as the class. Such a method can easily be mistaken to be a constructor.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructor with return value