• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

constructor with return value

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ();

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's not a constructor, that's a regular method that happens to have the same name as the class.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic