This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructor  RSS feed

 
RAMA KRISHNA AALLA
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we call a Constructor with in another constructor?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66188
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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.
 
vignesh hariharan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!