• Post Reply Bookmark Topic Watch Topic
  • New Topic

Two Constructers  RSS feed

 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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.


 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • 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.
 
Arun Thakur
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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"
 
Arun Maalik
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • 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
 
surbhigupta gupta
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
 
Amol Katyare
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!