469,306 Members | 2,504 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

calling constructors within constsructors

i have a class and a general constructor, then i have some other
constructors, each of which i'd like use the general constructor.

so in the other ones, i tried something like:

this = MyObject();

but that cannot work, as the variable "this" is final, so it cannot be
assigned anything.

can this be done (similarly to the super() call?).

and yes, i do know that i can wrap the general constructor up into a
private method and have each one of the constructors call that one... in
fact this is what i've already done... but i'm just curious if what i
wanted can be done more elegantly.

thanks,

murat

--
Murat Tasan
mx**@po.cwru.edu
ta***@eecs.cwru.edu
mu*********@cwru.edu
http://genomics.cwru.edu

Jul 17 '05 #1
3 10762
Try the following:

public class MyClass{
public MyClass(){
// simple constructor
}

public MyClass(String str){
this();
// do sth else

}
}

It is important that "this()" is the first statement in your complex
constructors.

Patrick

Jul 17 '05 #2
thanks for the suggestion, i totally forgot about that...

but then i found out it doesn't work in my situation anyhow... to use the
this(...) line in another constructor, it must be the first executed line
of that method... which doesn't help me.

so i think my alternative is the only way:

wrap the generic constructor into a private initialization method, make
the generic constructor only call this method, and then call this method
from the other constructors when needed.

thanks for the help,

murat

On Wed, 22 Oct 2003, Patrick Carl wrote:
Try the following:

public class MyClass{
public MyClass(){
// simple constructor
}

public MyClass(String str){
this();
// do sth else

}
}

It is important that "this()" is the first statement in your complex
constructors.

Patrick


--
Murat Tasan
mx**@po.cwru.edu
ta***@eecs.cwru.edu
mu*********@cwru.edu
http://genomics.cwru.edu

Jul 17 '05 #3
Joe
In article <Pine.SOL.4.53.0310221659350.12383@stewie>,
ta***@eecs.cwru.edu says...
so i think my alternative is the only way:


More often, it works the other way around:

public MyClass(String str){
// set instance variable to str
// do sth else

}
public class MyClass() {
this(String defaultStringValue);

}
}

--
"Everybody plays the fool. Sometimes."
-- American Folk Saying
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

42 posts views Thread by Edward Diener | last post: by
6 posts views Thread by Justin | last post: by
2 posts views Thread by Sathyaish | last post: by
4 posts views Thread by Sathyaish | last post: by
12 posts views Thread by st_ev_fe | last post: by
6 posts views Thread by daveb | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.