Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Calling constructor inside another constructor

Reply
Thread Tools

Calling constructor inside another constructor

 
 
Neroku
Guest
Posts: n/a
 
      11-16-2006
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

 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      11-16-2006

"Neroku" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.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


 
Reply With Quote
 
 
 
 
Jan =?ISO-8859-1?Q?Thom=E4?=
Guest
Posts: n/a
 
      11-16-2006
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
 
Reply With Quote
 
Doug Pardee
Guest
Posts: n/a
 
      11-16-2006
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.

 
Reply With Quote
 
trippy
Guest
Posts: n/a
 
      11-17-2006
In article <(E-Mail Removed). 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"



 
Reply With Quote
 
Mark Rafn
Guest
Posts: n/a
 
      11-17-2006
Neroku <(E-Mail Removed)> 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/t...ion.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.velocityreviews.com/forums/(E-Mail Removed) <http://www.dagon.net/>
 
Reply With Quote
 
Manoj Jain
Guest
Posts: n/a
 
      11-17-2006
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.

 
Reply With Quote
 
trippy
Guest
Posts: n/a
 
      11-17-2006
In article <(E-Mail Removed) .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"



 
Reply With Quote
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      11-18-2006
trippy <(E-Mail Removed)> 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().
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Calling Class Constructor from Another Constructor Alf P. Steinbach C++ 4 11-30-2007 09:56 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Calling own-constructors inside another constructor EdUarDo Java 4 12-21-2005 12:23 PM
Access a control inside an usercontrol from another control inside another usercontrol nail ASP .Net 0 09-15-2004 03:55 PM
Calling another constructor from a constructor Asfand Yar Qazi C++ 6 05-17-2004 03:16 PM



Advertisments