aspose file tools*
The moose likes Beginning Java and the fly likes Constructor of a constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor of a constructor" Watch "Constructor of a constructor" New topic
Author

Constructor of a constructor

Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77
Hi

This is my source code (it does not work):


class A {

A(A()) {
System.out.println("Constructor of a constructor");
}


public static void main (String [] args) {

A x = new A (A());


}
}


Is it possible to create a kind of a chain, that means, a constructor constructs a constructor?
The argument of the constructor should be a constructor. Maybe it is a kind of recursion.
Please give me some ideas. I want to clear my doubts.

Regards
Urs


SCJP 1.4
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Why would you want to do that?

A constructor can call another constructor in the same class, but why would you want to send a constructor as parameter?
Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77
Hi

It is for understanding.

How does a constructor call an other constructor? How does it work?

Regards
Urs
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
This is an example.

Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77
Hi

I am not quiet sure, if it works. I could not compile it. And it does not make a big sense.

private Test() { }

This is just a default constructor.

public Test(int num) {
this();
}

This is just an other constructor.

Regards
Urs
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by Urs Waefler:
Hi

I am not quiet sure, if it works. I could not compile it. And it does not make a big sense.

private Test() { }

This is just a default constructor.

public Test(int num) {
this();
}

This is just an other constructor.

Regards
Urs


The example should compile.

The first constructor is a no-argument constructor, not a default constructor.
Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77
Hi

After I could compile it. It worked. There are just two constructors. There is no relationship between the two constructors.

Still I try to find an answer to my initial question. Slightly I begin to realize, a constructor only constructs an object. A constructor, which calls a constructor does not extist probably? If it exits, maybe it does not make any sense in most of the cases.

Regards
Urs
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13864
    
  10

Can you explain exactly what you mean by a "constructor of a constructor"? A constructor is a special block of code inside a class which is called to initialise a new instance of the class. You cannot "construct a constructor".

What Keith shows you is this: Suppose you write a class and you want to provide multiple constructors (so that people can create instances of your class by supplying different parameters, for example). Suppose also that you need to do some work in the constructor to initialise the object properly. You don't want to copy and paste all that setup code in all your constructors. So what you can do is put the initialisation code in one constructor, and in the other constructors you call that one to do the initialisation. Calling another constructor inside a constructor is done with the this(...); call, as Keith shows.

Urs, you're asking a number of difficult questions here and I suspect you're making it more difficult than necessary. Make sure you understand exactly what classes, objects, constructors, methods, member variables etc. are and how they work. If you understand exactly what constructors are, you'll understand that your question above doesn't really make sense...


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor of a constructor
 
Similar Threads
Sequencing Declarations Question
Private Constructor
Why constuctor can't be marked as final?
doubt in Array construction
Make a class not to inherit and instantiate