Re: Do some computation before calling super constructor.
- From: Ian Shef <invalid@xxxxxxxxxxxxx>
- Date: Wed, 08 Feb 2006 19:29:33 GMT
"Oliver Wong" <owong@xxxxxxxxxxxxxx> wrote in
RJoGf.184829$AP5.103980@edtnps84:">news:RJoGf.184829$AP5.103980@edtnps84:
"Fabien Bergeret" <fabien.bergeret@xxxxxxxxxxxxxxxxxxxxxx> wrote in
message news:43ea0ac1$0$763$636a15ce@xxxxxxxxxxxxxxx
In a constructor, the super call MUST be the first line of your code.[...]
Therefore, you cannot call super after having made some computations.
What you can do in State :
public class State {
public State(){
}
public State(String name) {
super();
init(name);
}
protected void init(String name) {
this.name = name;
}
Some people claim you should never call methods that can be
overridden
from a constructor. I saw an example illustrating why this was
important, but I can't recall it off the top of my head right now.
Imagine that a subclass (class SubState extends State ...) overrides a
method used in the constructor. I believe what happens is that when the
superclass gets constructed, the new method gets used instead of the
original (now overriden) method. Thus the superclass does not get
initialized as expected. Furthermore, the new method may make use of
fields in the subclass that have not yet been initialized. This could be
very ugly.
This can be avoided by making "final" any methods used by a constructor.
The subclass cannot override a final method.
Did I get this right?
--
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *
.
- Follow-Ups:
- Re: Do some computation before calling super constructor.
- From: Ranganath Kini
- Re: Do some computation before calling super constructor.
- References:
- Do some computation before calling super constructor.
- From: Hendrik Maryns
- Re: Do some computation before calling super constructor.
- From: Fabien Bergeret
- Re: Do some computation before calling super constructor.
- From: Oliver Wong
- Do some computation before calling super constructor.
- Prev by Date: Re: Do some computation before calling super constructor.
- Next by Date: Re: How to load properties as resources?
- Previous by thread: Re: Do some computation before calling super constructor.
- Next by thread: Re: Do some computation before calling super constructor.
- Index(es):
Relevant Pages
|