Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-28-2009, 04:12 PM
Member
 
Join Date: Nov 2009
Posts: 9
Rep Power: 0
Somelauw is on a distinguished road
Default Calling a constructor from another one.
Code:
class Thing {

     int a_;

     public Thing(a) {
          a_ = a;
     }

     public Thing() {
          Thing(0); //This line doesn't work.
     }
How can I call a different version of a constructor from another constructor?
I could put the code in a private helpconstructor function, but I prefer something easier.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-28-2009, 04:42 PM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 1,262
Rep Power: 3
JosAH is on a distinguished road
Default
The parameter(s) of a constructor should be typed as well, e.g.

Code:
public Thing(int a) { this.a= a; }
public Thing() { this(0); } // <-- that's the way to call another c'tor
kind regards,

Jos
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Constructor call not calling Singing Boyo New To Java 5 06-09-2009 02:06 AM
Constructor calling ravian New To Java 2 12-22-2007 07:53 PM
Calling constructor of parent class from a constructor Java Tip Java Tips 0 12-19-2007 10:10 AM
Calling constructor of same class from a constructor Java Tip Java Tips 0 12-19-2007 10:01 AM
Constructor calling ordering JavaForums Java Blogs 0 12-07-2007 08:13 PM


All times are GMT +2. The time now is 11:03 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org