Re: Can a class's constructor call another constructor "later"?

From: Bruce Wood (brucewood_at_canada.com)
Date: 02/16/05


Date: 16 Feb 2005 10:17:11 -0800

I suppose that this is a "pit"... if, as Alex said in his 'blog, the
coffee is wearing off. I find the current constructor behaviour
(particularly as he wrote it in his class) perfectly obvious:

public SecurityManager(User user) : this() {
         this._user = user;
}

The call after the colon _always_ happens first. It has nothing to do
with "constructor overrides behave in the opposite way to method
overrides." I have no idea how he arrives at that conclusion. The
difference with constructor overrides is that the call order is
_fixed_: you can't insert a this() call or a base() call wherever you
like. It always comes first. The method override example he gave is
really a bit of a cheat because it calls the base method at the end,
illustrating nothing more than that when you override a method you can
choose to call the base method wherever you like, or not at all.

Constructor overrides, on the other hand, act like method overrides
that give you no choice where the base constructor is called: it is
always called at the beginning.

As well, I should point out that what Alex did in his first example
(the code with the bug in it, illustrated above) is bad constructor
form. Wherever possible, you should code constructors that take fewer
parameters to call those that take more parameters, _not_ the other way
around. His changed solution illustrates correct form:

public class SecurityManager {
      public SecurityManager () : this(null) {
      }

      public SecurityManager(User user) {
            this._user = user;
            if(user == null) {
                  this._user = User.CurrentUser;
            }
            InitialiseData();
      }
      private User _user;
}

The constructor with no parameters calls the one-parameter constructor,
passing it a default value for the parameter: in this case, a null. I
call this "correct form" because it is much easier to follow what's
going on, and far more difficult to make the kinds of mistakes that
Alex pointed out in his 'blog.



Relevant Pages

  • Re: Rules for constructors
    ... Alex Hunsley wrote: ... > constructor for A, the initialisation of the member variables in class B ... realising of GUI items from a constructor. ...
    (comp.lang.java.programmer)
  • Re: Do some computation before calling super constructor.
    ... Imagine that a subclass overrides a ... method used in the constructor. ... fields in the subclass that have not yet been initialized. ...
    (comp.lang.java.help)
  • Re: Initializing form (dialog)
    ... Alex wrote: ... some child form ... So I'm able to initialize controls in the ... someother function in between constructor and dlg.ShowDialog? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Constructor Chaining Conundrum
    ... that is not overriden by the child. ... s in Derived *shadows* s in Base, ... printin Derived *overrides* printin Base; ... constructor calls print, Derived's printwill be called (we are ...
    (comp.lang.java.programmer)
  • Re: Extend a class without calling the constructor
    ... I plan to do this by creating a class B which extends A, and overrides the method. ... really have to then make the overriding method correct even before you constructor has executed. ... There are hacks with thread-locals, synchronised statics or inner classes to hand context information over. ...
    (comp.lang.java.programmer)

Quantcast