• 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

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that Constructors dont have a return type.... i would like to know WHY??
 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They do.

It's an instance of the Class being "constructed", just not specified on the constructor signature.

What else are you "constructing" ?

WP
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If they did return a type (other than the class being constructed), how would you save both? currently you do this:

now, assume the FooBar constructor returned an int. How do you save the returned int AND the reference to the new object?

 
Prashant Ameta
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sir,
I am quite new to JAVA,i am following Head first Java...and it says Constructors dont have a return type , not even void...and same is the difference between a method and a Constructor....

Now , after a month or two,if an interviewer asks me "Does a COnstructor have a return type or not??" I shall say Yes,Right???
 
Prashant Ameta
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot
Its crystal clear Now..
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prashant Ameta wrote:Now , after a month or two,if an interviewer asks me "Does a COnstructor have a return type or not??" I shall say Yes,Right???


I would say "It doesn't have a DECLARED return type, but it does return a reference to the newly created object".
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prashant Ameta wrote:Its crystal clear Now..:)



I'm glad you think so.

Because it certainly isn't to me, after reading those answers. The way I see it is this: when you declare a constructor in Java, you don't declare a return type, otherwise it would be a method. That much is clear. So does that mean that a constructor doesn't "have" a return type?

It also isn't true that a constructor returns an object of the class in which it's declared. Consider this code:



Now as you should already have learned if you're going to be asked questions like that, the FooBar() constructor implicitly has a line of code



inserted as its first line by the compiler. That line of code causes its superclass's constructor to be called. And since the superclass is Object, that constructor is



Now, notice that although this Object constructor is called, it doesn't return an Object. All it does is to initialize the Object class's state in the FooBar object which is being initialized. And likewise the FooBar constructor doesn't return a FooBar, all it does is to initialize the FooBar class's state in the FooBar object. So in fact a constructor doesn't return anything, all it does is to initialize the state of an object.

So what is it which does return a FooBar object? It's the "new" operator which does that.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

William P O'Sullivan wrote:They do.



No, they don't. The new operator creates the object and returns a reference to it. The c'tor merely initializes the existing, newly created object. It doesn't return anything.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How is the reference returned when contructor is created and what happens when we dont create constructor for any program??
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raja Pathak wrote:How is the reference returned when contructor is created



As already stated, twice, the new operator returns a reference to the newly created object.

and what happens when we dont create constructor for any program??



If you don't define any constructor, the compiler defines one for you that takes no arguments and does nothing except call super().
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The constructor does not really return the reference. A constructor is just a special block of code that's called to initialize a new object. As Jeff says, the object itself isn't created by the constructor - it's actually the new operator that creates it, and it calls the constructor to initialize the new object.

If you don't provide any constructors in your class, then the Java compiler will automatically add one for you that looks like this:

Reference: Java Language Specification section 8.8.9
 
reply
    Bookmark Topic Watch Topic
  • New Topic