• 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: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we call a Constructor with in another constructor?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is not an advanced question. Please post in a more appropriate forum.

Also, please use mixed case for your display name, and please title your topics with more care. This is the second topic you've posted with the title "Constructor". How are we to tell them apart?

Please read this for more info.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:
[QB]...This is the second topic you've posted with the title "Constructor". How are we to tell them apart?


Indeed. Note that you can edit your original post by clicking on the pen/pencil icon.

Within a constructor, the keyword "this" (followed by a parameter list) is used to call overloaded constructors within the same class. If you do this, it must be the first line of the constructor.

For example, the following constructor takes an int, then calls an overloaded version that takes an int and a String.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya.. but there exist one ambiguity problem over here.. eventhough u use this() you r supposed to be careful of using super too..

eg:


and also the reverse case is also throws error.. that is if u put super() in the first line and this(int i) in the second.. that will throw the error saying this(int i) should be the first line of the constructor.

so programmers are supposed to be careful in using this and super inside a constructor
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic