Results 1 to 2 of 2
  1. #1
    Somelauw is offline Member
    Join Date
    Nov 2009
    Posts
    10
    Rep Power
    0

    Default Calling a constructor from another one.

    Java 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.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    The parameter(s) of a constructor should be typed as well, e.g.

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

    Jos

Similar Threads

  1. Constructor call not calling
    By Singing Boyo in forum New To Java
    Replies: 5
    Last Post: 06-09-2009, 01:06 AM
  2. Constructor calling
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-22-2007, 06:53 PM
  3. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  4. Replies: 0
    Last Post: 12-19-2007, 09:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •