Calling constructor inside another constructor

N

Neroku

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
 
O

Oliver Wong

Neroku said:
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
 
J

Jan =?ISO-8859-1?Q?Thom=E4?=

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

 
D

Doug Pardee

Neroku said:
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.
 
T

trippy

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"
 
M

Mark Rafn

Neroku said:
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.
 
M

Manoj Jain

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

trippy

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"
 
T

Tor Iver Wilhelmsen

trippy said:
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().
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top