I am new to this site and really like this site a lot
Its creative and informative.
After watching this discussion group for a time, I thought to get involved too..
Below is the constructor concepts that I have collected(from books and discussion groups(mostly javaranch),
and I thought it will be very helpful for those preparing the exam. Please correct me..if there be any mistake.
I also invite others to share concept based tips.If you all find it useful, I will post more like this...
1. A private constructor is legal and can be used to implement a Singleton.
2. constructors can ONLY be public/private/protected
3.constructor cannot be
native, abstract, final, synchronized, static
4.constructors cannot be final :
The keyword final when dealing with methods means the method cannot be overridden. Because constructors are never inherited and so will never have the opportunity to be overridden, final would have no meaning to a constructor.
5.Overloading constructors is a key technique to allow multiple ways of initializing classes.
6.some constructor rules:
(i)If you do not explicitly create a Constructor on a class a default No Arg constructor IS created
(ii)If you do create a customized Constructor with a Arg/Args, the default no arg will NOT be created
(iii)IF you create a Constructor in SubClass with or without arg, a call to super() ( the default no arg constructor of the parent) IS IMPLICITLY called unless you explicitly call super(args).
(iv)When you explicitly call a Parents constructor in the childs constructor it has to be the first line of code.
7.Applet with constructors:
constructor will be called before init. But be careful about using Applet stuff - it might not all be initialized yet. Initialization stuff should be performed in the init() because of something to do with the environment of the applet not being ready yet, when executing a constructor for the applet.
8) Math class have private constructors
The class is final but the constructor is private, that is why you cannot find any constructor in the API Doc.
9) Instance initializers are processed before constructors are invoked. Every class whether it is a superclass to some class or a subclass to another class can have static initializers as well as instance initializers.In each class the order of initialization is
1.All static initializers are executed from top to bottom of the inheritance hierarchy first.
2. Next From top to bottom , in each class the instance initialization and constructor code are executed at once and it goes down to next level.
3. The 2nd step is continued upto the bottom most class.
10) An anonymous class cannot have any constructors.
An anonymous class can only be created within the body of a method. That implies , A constructor is not a method and it can have anonymous classes declared in it.
11) a constructor may throw exception
One precaution there...
Per Bruce Eckel (TIJ, P-392) -
If your class is a derived class then its constructor must declare any base-class constructor exceptions in its exception specification.
Because a base-class constructor will always be called one way or another. ...and you can't put it inside a try loop, though you might want to.
12) abstract class MAY have constructors. example Component()
13) super and this can�t appear in the same constructor
14) The compiler never object to creating a local instance of the class being constructed in its constructor. If the ca;; produce an infinite loop, a runtime error will occur.
15) Constructors can�t be overridden. They can be overloaded, but only in the same class.