Constructor : Constructor « Object Oriented « SCJP






Every class, even an abstract class, has at least one constructor.

Constructors must have the same name as the class.

Constructors don't have a return type. 

Constructors can use any access modifier, even private!.

The compiler will create a default constructor if you don't create any constructors in your class.

The default constructor is a no-arg constructor with a no-arg call to super().

A constructor is always invoked when a new object is created.

The constructor calls its superclass constructor, which calls its superclass constructor.

The first statement of every constructor must be a call to either this() (an overloaded constructor) or super().

The compiler will add a call to super() unless you have already put in a call to this() or super().

Instance members are accessible only after the super constructor runs.

Abstract classes have constructors that are called when a concrete subclass is instantiated.

If your superclass does not have a no-arg constructor, 
you must create a constructor and insert a call to super() with arguments matching those of the superclass constructor.

Constructors are never inherited, thus they cannot be overridden.








6.7.Constructor
6.7.1.Constructor
6.7.2.super() and this()
6.7.3.Constructors have no return type and their names must exactly match the class name.
6.7.4.Constructors are used to initialize instance variable state, as follows:
6.7.5.Constructors can use any access modifier, including private.
6.7.6.How compiler generates the constructor
6.7.7.What happens if the super constructor has arguments?
6.7.8.Overloaded Constructors
6.7.9.A constructor is invoked with a call of the form new YourClassName(arg1, arg2 ,... ).
6.7.10.The constructor is called a no-arguments constructor if the argument list is empty.
6.7.11.If you do not explicitly code any constructors, the compiler automatically creates a default constructor
6.7.12.You can pass control to a constructor in the parent class by using the keyword super.
6.7.13.To control the particular constructor that is used, you simply provide the appropriate arguments.
6.7.14.Overloading Constructors
6.7.15.Use this to call overloaded constructor
6.7.16.Java insists that the object is initialized from the top of the class hierarchy downward
6.7.17.Compiler puts in a call to the no-argument constructor in the parent
6.7.18.If you extend a class without no-argument constructor, then you must explicitly call super().
6.7.19.Constructors Throwing Exceptions
6.7.20.Invoking constructors in the parent class must be in the first line of code in the constructor body.
6.7.21.A class can have multiple constructors, but they must have different parameter lists.
6.7.22.A constructor can invoke another constructor, this must be done in the first line of code in the constructor body.
6.7.23.The only way to abort a constructor is to throw an exception.