Calling constructor inside another constructor  
Author Message
Neroku





PostPosted: 2006-11-17 0:59:00 Top

java-programmer, Calling constructor inside another constructor 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

 
Oliver Wong





PostPosted: 2006-11-17 1:01:00 Top

java-programmer >> Calling constructor inside another constructor
"Neroku" <email***@***.com> wrote in message
news:email***@***.com...
> 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


 
Jan Thom





PostPosted: 2006-11-17 1:39:00 Top

java-programmer >> Calling constructor inside another constructor 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
 
 
Doug Pardee





PostPosted: 2006-11-17 7:30:00 Top

java-programmer >> Calling constructor inside another constructor 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.

 
 
trippy





PostPosted: 2006-11-17 9:36:00 Top

java-programmer >> Calling constructor inside another constructor In article <email***@***.com>,
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"



 
 
Manoj Jain





PostPosted: 2006-11-17 13:02:00 Top

java-programmer >> Calling constructor inside another constructor 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.

 
 
trippy





PostPosted: 2006-11-17 14:09:00 Top

java-programmer >> Calling constructor inside another constructor In article <email***@***.com>,
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"



 
 
Tor Iver Wilhelmsen





PostPosted: 2006-11-18 18:20:00 Top

java-programmer >> Calling constructor inside another constructor trippy <email***@***.com> 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().