Calling constructor inside another constructor

Discussion in 'Java' started by Neroku, Nov 16, 2006.

  1. Neroku

    Neroku Guest

    Hello, I have a problem calling a constructor inside another
    constructor, consider this code:

    class Point
    {
    private final int Y_DEFAULT = 3;
    private final int Z_DEFAULT = 5;
    private int x,y,z;

    Point(int x)
    {
    this(x,Y_DEFAULT,Z_DEFAULT);
    }
    Point(int x, int y, int z)
    {
    this.x = x;
    this.y = y;
    this.z = z;
    }
    }

    I get these errors whe I try to compile the code above:

    cannot reference Y_DEFAULT before supertype constructor has been called
    cannot reference Z_DEFAULT before supertype constructor has been called

    Does anybody know why this happens?

    TIA
     
    Neroku, Nov 16, 2006
    #1
    1. Advertising

  2. Neroku

    Oliver Wong Guest

    "Neroku" <> wrote in message
    news:...
    > Hello, I have a problem calling a constructor inside another
    > constructor, consider this code:
    >
    > class Point
    > {
    > private final int Y_DEFAULT = 3;
    > private final int Z_DEFAULT = 5;
    > private int x,y,z;
    >
    > Point(int x)
    > {
    > this(x,Y_DEFAULT,Z_DEFAULT);
    > }
    > Point(int x, int y, int z)
    > {
    > this.x = x;
    > this.y = y;
    > this.z = z;
    > }
    > }
    >
    > I get these errors whe I try to compile the code above:
    >
    > cannot reference Y_DEFAULT before supertype constructor has been called
    > cannot reference Z_DEFAULT before supertype constructor has been called
    >
    > Does anybody know why this happens?


    Maybe it's because you can't reference Y_DEFAULT or Z_DEFAULT before the
    supertype constructor has been called? ;)

    Does your design break if you declare those fields as being static?

    - Oliver
     
    Oliver Wong, Nov 16, 2006
    #2
    1. Advertising

  3. Or,

    if you like the hackish way (no i dont recommend to do this, it's just for
    educational purposes, by all means go with the statics):

    class Point {
    private int x;
    private int y = 3, z = 5;

    Point( int x ) {
    this.x = y;
    }

    Point( int x, int y, int z) {
    this( x );
    this.y = y;
    this.z = z;
    }
    }

    Greetings,
    Jan

    Oliver Wong wrote:

    >> class Point
    >> {
    >> private final int Y_DEFAULT = 3;
    >> private final int Z_DEFAULT = 5;
    >> private int x,y,z;
    >>
    >> Point(int x)
    >> {
    >> this(x,Y_DEFAULT,Z_DEFAULT);
    >> }
    >> Point(int x, int y, int z)
    >> {
    >> this.x = x;
    >> this.y = y;
    >> this.z = z;
    >> }
    >> }


    --
    __________________________________________________________
    insOMnia - We never sleep...
    http://www.insomnia-hq.de
     
    Jan =?ISO-8859-1?Q?Thom=E4?=, Nov 16, 2006
    #3
  4. Neroku

    Doug Pardee Guest

    Neroku wrote:
    > private final int Y_DEFAULT = 3;
    > private final int Z_DEFAULT = 5;


    Make these private static final int and you'll be fine.

    You need the "static" in there.
     
    Doug Pardee, Nov 16, 2006
    #4
  5. Neroku

    trippy Guest

    In article <>,
    Neroku took the hamburger meat, threw it on the grill, and I said "Oh
    Wow"...

    > Hello, I have a problem calling a constructor inside another
    > constructor, consider this code:
    >
    > class Point
    > {
    > private final int Y_DEFAULT = 3;
    > private final int Z_DEFAULT = 5;
    > private int x,y,z;
    >
    > Point(int x)
    > {
    > this(x,Y_DEFAULT,Z_DEFAULT);
    > }
    > Point(int x, int y, int z)
    > {
    > this.x = x;
    > this.y = y;
    > this.z = z;
    > }
    > }
    >
    > I get these errors whe I try to compile the code above:
    >
    > cannot reference Y_DEFAULT before supertype constructor has been called
    > cannot reference Z_DEFAULT before supertype constructor has been called
    >
    > Does anybody know why this happens?


    2 things:

    1) You have to call super first in the constructor, supplying the same
    arguments as the subclass.

    2) public class Point extends YourSuperclass {

    }



    --
    trippy
    mhm31x9 Smeeter#29 WSD#30
    sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

    NP: "All I Really Want" -- Alanis Morissette

    "Now, technology's getting better all the time and that's fine,
    but most of the time all you need is a stick of gum, a pocketknife,
    and a smile."

    -- Robert Redford "Spy Game"
     
    trippy, Nov 17, 2006
    #5
  6. Neroku

    Mark Rafn Guest

    Neroku <> wrote:
    >Hello, I have a problem calling a constructor inside another
    >constructor


    No, you have a problem referencing a member variable before the class has been
    initialized.

    >class Point
    >{
    > private final int Y_DEFAULT = 3;
    > private final int Z_DEFAULT = 5;
    > private int x,y,z;
    >
    > Point(int x)
    > {
    > this(x,Y_DEFAULT,Z_DEFAULT);
    > }
    > Point(int x, int y, int z)
    > {
    > this.x = x;
    > this.y = y;
    > this.z = z;
    > }
    >}


    >cannot reference Y_DEFAULT before supertype constructor has been called
    >cannot reference Z_DEFAULT before supertype constructor has been called


    You've learned one of the subtleties of java class instantiation. See
    http://java.sun.com/docs/books/jls/third_edition/html/execution.html#44670

    Java classes are instantiated in the following order:

    (at classload time)
    0. initializers for static members and static initializer blocks, in order
    of declaration.
    (at each new object)
    1. create local variables for constructor arguments
    2. if constructor begins with invocation of another constructor for the
    class, evaluate the arguments and recurse to previous step. All steps
    are completed for that constructor, including further recursion of
    constructor calls, before continuing.
    3. if the superclass hasn't been constructed by the above, construct the
    the superclass (using the no-arg constructor if not specified). Like #2,
    go through all of these steps for the superclass, including constructing
    IT'S superclass, before continuing.
    4. initializers for instance variables and non-static initializer blocks, in
    order of declaration.
    5. rest of the constructor.

    >Does anybody know why this happens?


    You're trying to use variables Y_DEFAULT and Z_DEFAULT in step 2, but they're
    not initialized until step 4. You can't do that.

    You could make these variables static, if that works for your logic. For
    constants, it's good habit. If they need to be instance rather than class
    variables, you'll probably want to move your assignments to a private init(int
    x, int y, int z) method, and just call init from both constructors.
    --
    Mark Rafn <http://www.dagon.net/>
     
    Mark Rafn, Nov 17, 2006
    #6
  7. Neroku

    Manoj Jain Guest

    When you call the constructor with argument, in argument, it expects
    variables other than its member. It thinks the member variables haven't
    been created yet. Thats why, when you either call the constructor with
    its member variables in argument or create the object giving member
    variables in argument, it gives error like "cannot reference member
    variable before supertype constructor has been called" or "member
    variable might not have been initialized" respectively for the same
    reason.
     
    Manoj Jain, Nov 17, 2006
    #7
  8. Neroku

    trippy Guest

    In article <>,
    Manoj Jain took the hamburger meat, threw it on the grill, and I said
    "Oh Wow"...

    > When you call the constructor with argument, in argument, it expects
    > variables other than its member. It thinks the member variables haven't
    > been created yet. Thats why, when you either call the constructor with
    > its member variables in argument or create the object giving member
    > variables in argument, it gives error like "cannot reference member
    > variable before supertype constructor has been called" or "member
    > variable might not have been initialized" respectively for the same
    > reason.
    >
    >


    Ah. Thanks.

    --
    trippy
    mhm31x9 Smeeter#29 WSD#30
    sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

    NP: "All I Really Want" -- Alanis Morissette

    "Now, technology's getting better all the time and that's fine,
    but most of the time all you need is a stick of gum, a pocketknife,
    and a smile."

    -- Robert Redford "Spy Game"
     
    trippy, Nov 17, 2006
    #8
  9. trippy <> writes:

    > 1) You have to call super first in the constructor, supplying the same
    > arguments as the subclass.


    Not when he calls another constructor in the same class instead. So
    you need to start with a call to this() or super(), but you can keep
    using this() as long as at least one constructor in the "chain" calls
    super().
     
    Tor Iver Wilhelmsen, Nov 18, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nail
    Replies:
    0
    Views:
    1,564
  2. EdUarDo
    Replies:
    4
    Views:
    22,730
    Roedy Green
    Dec 21, 2005
  3. Asfand Yar Qazi
    Replies:
    6
    Views:
    15,671
    jeffc
    May 17, 2004
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,286
  5. Alf P. Steinbach
    Replies:
    4
    Views:
    419
    James Kanze
    Nov 30, 2007
Loading...

Share This Page