Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Using multiple constructors from a class.

  1. #1
    BradMD is offline Member
    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Default Using multiple constructors from a class.

    Hello. I have a simple question about using multiple constructors. If the instance of an object is created from a class with multiple constructors, how does the compiler know which constructor to use if each constructor uses the same number of parameters?

    This is just a code I will use for an example. I do not have a problem with any current code, but I would really appreciate an explanation of how to do this.
    I know that the code is useless, but I wrote it to illustrate the question I have.

    Main.java
    Java Code:
    import project.Shape
    public class Main{
    
    public static void main (String args []) {
    
    Shape myShape = new Shape(5, 2);
    
    }}
    Shape.java
    Java Code:
    public class Shape {
    
    public Shape (int w, int h) {
    width = w;
    height = h;
    }
    public Shape (int x, int y( {
    origin.x = x;
    origin.y = y;
    }}
    Please excuse any errors. This is just an idea of what is going on in my head right now. If there are two constructors in Shape.java, then, if I create an object in Main.java with two parameters, which constructor will be used (if any)?

    Thanks for any answer :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Using multiple constructors from a class.

    Your code is worse than useless -- it won't compile. Excusing typographical errors, the compiler will have no way of knowing which constructor you mean if they have the exact same signature: Shape ( int, int).

    In order to use multiple constructors, they must have different signatures -- their parameter list must be different. In your example above, both constructors use int, int, and again, the compiler will not allow this. But you can modify the above code, correct the typographical errors and errors of omission, and get something that may be useful. For instance if one constructor sets x and y, and the other sets x, y, and height and width, this can work. e.g.,

    Java Code:
    import java.awt.Point;
    
    public class Shape {
    
       private int width;
       private int height;
       private Point origin;
    
       public Shape(int x, int y) {
          origin.x = x;
          origin.y = y;
       }
    
       public Shape(int x, int y, int w, int h) {
          this(x, y);
          width = w;
          height = h;
       }
    }
    Now the two constructors have different signatures: Shape(int, int) and Shape(int, int, int, int), and this will compile and work, and possibly be useful.
    Last edited by Fubarable; 08-08-2012 at 05:33 PM.
    awinston likes this.

  3. #3
    BradMD is offline Member
    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Default Re: Using multiple constructors from a class.

    Okay, I see. Thank you for helping me understand.
    Last edited by BradMD; 08-08-2012 at 05:34 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Using multiple constructors from a class.

    Please see the edit to my answer above.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,995
    Blog Entries
    7
    Rep Power
    19

    Default Re: Using multiple constructors from a class.

    Quote Originally Posted by BradMD View Post
    Okay, so if I were to use this as a program, I would just combine constructors then and use 4 params instead, right?
    Nope, constructors are not virtual but they can be overloaded; overloading is a compiler thingy and it decides which method or constructor to use by checking the number of parameters as well as their types. So (int, int) can't be distinguished from (int, int) so the compiler will complain about those parameter lists. I don't know what four parameters have to do with this all ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    2

    Default Re: Using multiple constructors from a class.

    Quote Originally Posted by JosAH View Post
    Nope, constructors are not virtual but they can be overloaded; overloading is a compiler thingy and it decides which method or constructor to use by checking the number of parameters as well as their types. So (int, int) can't be distinguished from (int, int) so the compiler will complain about those parameter lists. I don't know what four parameters have to do with this all ...

    kind regards,

    Jos
    I'm pretty sure that he was talking about the constructor with x, y, width, and height as the 4 parameters.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Using multiple constructors from a class.

    My $0.02
    Java Code:
    class Foo {
        Foo(String s, int x) {}
    
        Foo(int x, String s) {}
    }
    For constructors (and methods) you can do the above. Even though they both take a String and an int, the order is different for each constructor.

Similar Threads

  1. Replies: 11
    Last Post: 03-05-2012, 11:18 AM
  2. Class and constructors question.
    By Meta in forum New To Java
    Replies: 19
    Last Post: 04-29-2010, 02:58 AM
  3. Define class and constructors
    By HaDesT in forum New To Java
    Replies: 2
    Last Post: 03-04-2010, 05:02 PM
  4. Replies: 1
    Last Post: 05-19-2009, 11:03 PM
  5. different multiple public class and main class
    By mr idiot in forum New To Java
    Replies: 2
    Last Post: 01-01-2009, 12:10 PM

Posting Permissions

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