• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Two Constructers

 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

I am writing a code which is giving an ambiguios solution the code is as follows:



Now please suggest me what should be the output and also give me the reason.


 
author
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Obviously it will print "In simple constructor"

If you have a rutun type like "void" then it is not a constructor anymore. it is a method with a duplicate name as the constructor.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ak pillai is right.

with the void its is no longer a constructer but a method which is returning nothing.

So it will print "In simple constructor"
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey dear
It will print only that "In Simple Constructor". But well why see as long as U don't specify any return type even void with a constructor it is constructor. But when U specify a return type with a constructor it is no longer constructor it is just a memeber function of that class having duplicate name as constructor.
Did U understood? Hop U will get . By then tack care pls
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes it will not remain any more as constructor if we write the void type before it , because a constructor is having a class name with no return type specified , other wise it will become a method
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is possible to have a method with the same name as constructor because JVM uses different name spaces for methods and constructor. The one with return type is considered as method.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can even do the following.

I don't know if it's possible to add package X to this
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic