Constructor calling

J

Justin Kaeser

Hello helpful folks,

Is there a way to call a classes default constructor from another
constructor *after* calling it's superconstructor? The scenario is
somewhat like this:

class Foo
{
Bdauh augh;
String name;

Foo()
{ name = "Mr. Foo" }
Foo(Bdauh a)
{ augh = a }
{

class Bar extends Foo
{
Bar()
{ name = "Mr. Bar" }
Bar(Bdauh a)
{
super(a);
// after this I want to call Bar()
}
}

Now I want to create a Bar object with a Bdauh as parameter.
If I call this() from Foo, the name of the constructed object will be
"Mr. Foo".
Calling this() from Bar(Bdauh a) on the other hand won't work at all
after calling super(a). So how can I call the default constructor? :)
Or do I have to work around it using an init() method which is called
from the Foo constructor?
 
C

Chris Smith

Justin said:
class Bar extends Foo
{
Bar()
{ name = "Mr. Bar" }
Bar(Bdauh a)
{
super(a);
// after this I want to call Bar()
}
}

No, you can't do that. What you can do is move the code out of Bar()
into a private method, and call it from both places, as such:

class Bar extends Foo
{
Bar()
{
init();
}

Bar(Bdauh a)
{
super(a);
init();
}

private void init()
{
name = "Mr. Bar";
}
}

Of course, that's only worthwhile if you've got a substantial amount of
code there. It's probably overkill for one line in two constructors;
I'd just repeat the line.

Another possibility, more specific to your example, is to move the
initial value into a constant, if you want to insulate against possible
changes to it:

class Bar extends Foo
{
private final String INIT_NAME = "Mr. Bar";

Bar()
{
name = INIT_NAME;
}

Bar(Bdauh a)
{
super(a);
name = INIT_NAME;
}
}

HTH,

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
D

Daniel Bonniot

Hi,
class Foo
{
Bdauh augh;
String name;

Foo()
{ name = "Mr. Foo" }
Foo(Bdauh a)
{ augh = a }

Did you try instead:

Foo(Bdauh a)
{ this(); augh = a }

A difference for class Foo is that now new Foo(a).name will be equal to
"Mr. Foo", while in your original code it was null. But the new
behaviour sems a priori more logical.

Cheers,

Daniel
The Nice programming language: http://nice.sf.net
 
M

Marco

Justin said:
class Bar extends Foo
{
Bar()
{ name = "Mr. Bar" }
Bar(Bdauh a)
{
super(a);
// after this I want to call Bar()
}
}

I prefer dumping all my initialization code into one
"master" constructor. Any other constructors are just
"slaves" (interfacial conveniences) that delegate to the
"master". Example...

class Foo {
private static final String DEFAULT_NAME = "Mr. Foo";
Bdauh augh;
String name;
Foo() { this(null); }
Foo(Bdaugh a) { this(a, DEFAULT_NAME); }
Foo(Bdaugh a, String name) { // MASTER constructor
augh = a;
this.name = name;
}
}

class Bar extends Foo {
private static final String DEFAULT_NAME = "Mr. Bar";
Bar() { this(null); }
Bar(Bdauh a) { super(a, DEFAULT_NAME); }
}

Marco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top