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 / December 2005

Tip: Looking for answers? Try searching our database.

Calling own-constructors inside another constructor

Thread view: 
EdUarDo - 21 Dec 2005 09:09 GMT
Hi all,

Inspecting some pieces of code from someone I found things like this:

class A {
    private String id = "";

    public A() {
        super();
    }

    public A(String id) {
        this.id = id;
    }
}

class B extends A {
    public B() {
        this(""); <-------------------- HERE!
    }

    public B(String id) {
        super(id);
    }
}

Inside constructor B() it's calling a constructor. Could this cause some collateral or strange problems?
I know it's nonsense because id attribute it's being initialized by default to "", but I'd like to know if
also is a problem.
Thomas Weidenfeller - 21 Dec 2005 09:45 GMT
> class B extends A {
>     public B() {
[quoted text clipped - 8 lines]
> Inside constructor B() it's calling a constructor. Could this cause some
> collateral or strange problems?

No, it is a common idiom. If you don't use this(), Java will silently
insert a call to the no-arg superclass constructor. Instead of geting
that default behavior, the usage of this() allows to specify some more
clever bahaviour.

You probably think about calling ordinary methods from a constructor.
That, if not properly done, can cause problems.

/Thomas
Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Roedy Green - 21 Dec 2005 10:21 GMT
On Wed, 21 Dec 2005 10:45:51 +0100, Thomas Weidenfeller
<nobody@ericsson.invalid> wrote, quoted or indirectly quoted someone
who said :

>> class B extends A {
>>     public B() {
[quoted text clipped - 16 lines]
>You probably think about calling ordinary methods from a constructor.
>That, if not properly done, can cause problems.

If you have a B(String id) constructor, Java WON'T create the default
no-arg constructor for you. If you don't write a no-arg constrictor,
and go ahead and attempt to use it anyway, Java will give you a
compile time error. Java only creates the no-arg constructor  if you
have no constructors defined.

A constructor calling another with this(...) is common to provide
default parameters.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

EdUarDo - 21 Dec 2005 10:31 GMT
> A constructor calling another with this(...) is common to provide
> default parameters.

But since the attribute is already initialized to "" in the superclass, why don't use super() instead of this("")?
Roedy Green - 21 Dec 2005 12:23 GMT
On Wed, 21 Dec 2005 11:31:37 +0100, EdUarDo
<eduardo.yanezNOSPAM@NOSPAMgmail.com> wrote, quoted or indirectly
quoted someone who said :

>> A constructor calling another with this(...) is common to provide
>> default parameters.
>
>But since the attribute is already initialized to "" in the superclass, why don't use super() instead of this("")?

Because this("") is more encapsulated. It depends only on the
behaviour of the current class, not the superclass.  You want to
create as few interdependencies between classes as possible and you
don't want to write code that depends on undocumented current
behaviour. That could easily change and your code would mysteriously
stop working.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



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.