• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Constructor of a constructor

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

It is for understanding.

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

Regards
Urs
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is an example.

 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
reply
    Bookmark Topic Watch Topic
  • New Topic