Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

Calling constructor inside another constructor

Thread view: 
Neroku - 16 Nov 2006 16:59 GMT
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 - 16 Nov 2006 17:01 GMT
> Hello, I have a problem calling a constructor inside another
> constructor, consider this code:
[quoted text clipped - 23 lines]
>
> 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ä - 16 Nov 2006 17:38 GMT
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    

>> class Point
>> {
[quoted text clipped - 13 lines]
>>  }
>> }

Signature

__________________________________________________________
insOMnia - We never sleep...
http://www.insomnia-hq.de

Doug Pardee - 16 Nov 2006 23:30 GMT
>   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 - 17 Nov 2006 01:36 GMT
In article <1163696346.192529.25080@h54g2000cwb.googlegroups.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:
[quoted text clipped - 23 lines]
>
> 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 {

}

Signature

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 - 17 Nov 2006 05:02 GMT
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 - 17 Nov 2006 06:08 GMT
In article <1163739728.283847.314340@k70g2000cwa.googlegroups.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
[quoted text clipped - 4 lines]
> variable might not have been initialized"  respectively for the same
> reason.

Ah. Thanks.

Signature

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 - 18 Nov 2006 09:36 GMT
> 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().
Mark Rafn - 17 Nov 2006 04:05 GMT
>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
>{
[quoted text clipped - 13 lines]
>  }
>}

>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    dagon@dagon.net    <http://www.dagon.net/>


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.