Every class, including abstract classes, MUST have a constructor. Objects are constructed. You can't make a new object without invoking a constructor. In fact, you can't make a new object without invoking not just the constructor of the object's actual class type, but also the constructor of each of its super classes! Constructors are the code that runs whenever you use the keyword new. OK, to be a bit more accurate, there can also be initialization blocks that run when you say new. Object instance variables are given their explicit values. By explicit values, we mean values that are assigned at the time the variables are declared, like "int x =27", where "27" is the explicit value (as opposed to the default value) of the instance variable.
A private constructor means only code within the class itself can instantiate an object of that type, so if the private constructor class wants to allow an instance of the class to be used, the class must provide a static method or variable that allows access to an instance created from within the class.
It's legal (but stupid) to have a method with the same name as the class, but
that doesn't make it a constructor. If you see a return type, it's a method rather than a constructor. In fact, you could have both a method and a constructor with the same name—the name of the class—in the same class, and that's not a problem for Java. Be careful not to mistake a method for a constructor—be sure to look for a return type.
Every constructor has, as its first statement, either a call to an overloaded
constructor (this()) or a call to the super class constructor (super()), although
remember that this call can be inserted by the compiler.
If you do type in a constructor (as opposed to relying on the compiler generated
default constructor), and you do not type in the call to super() or a call to
this(), the compiler will insert a no-argument call to super() for you, as the very first statement in the constructor.
A call to super() can be either a no-argument call or can include arguments passed
to the super constructor.
A no-argument constructor is not necessarily the default (i.e., compiler-supplied)
Constructor, although the default constructor is always a no argument constructor. The default constructor is the one the compiler provides! While the default constructor is always a no-argument constructor, you're free to put in your own no argument constructor. You cannot make a call to an instance method, or access an instance variable, until after the super constructor runs.
Only static variables and methods can be accessed as part of the call to
super() or this(). (Example: super (Animal.NAME) is OK, because NAME is
declared as a static variable.)
Abstract classes have constructors, and those constructors are always called
when a concrete subclass is instantiated.
Interfaces do not have constructors. Interfaces are not part of an object's
inheritance tree.
The only way a constructor can be invoked is from within another constructor.
In other words, you can't write code that actually calls a constructor.
How do you Know What the Default Constructor will Look Like?
Because…
_ The default constructor has the same access modifier as the class.
_ The default constructor has no arguments.
_ The default constructor includes a no argument call to the super constructor
(super()).
If your super constructor (that is, the constructor of your immediate super class/parent) has arguments, you must type in the call to super( ), supplying the appropriate arguments.
Crucial point: If your super class does not have a argument constructor, you must type a constructor in your class (the subclass) because you need a place to put in the call to super with the appropriate arguments.
One last point on the whole default constructor thing (and it's probably very obvious, but we have to say it or we'll feel guilty for years), constructors are never inherited. They aren't methods. They can't be overridden (because they aren't methods and only instance methods can be overridden). So the type of constructor(s) your super class has in no way determines the type of default constructor you'll get. Some folks mistakenly believe that the default constructor somehow matches the super constructor, either by the arguments the default constructor will have (remember, the default constructor is always a no-arg), or by the arguments used in the compiler-supplied call to super(). So, although constructors can't be overridden, you've already seen that they can be
Overloaded.
Overloading a constructor is typically used to provide alternate ways for clients to
instantiate objects of your class. this() always means a call to another constructor in the same class.
Key Rule: The first line in a constructor Must be a call to super() or a call to this().
Constructor can never have both a call to super() and a call to this(). Because each of those calls must be the first statement in a constructor, you can't legally use both in the same constructor. That also means the compiler will not put a call to super() in any constructor that has a call to this().
The benefit of having overloaded constructors is that you offer flexible ways to instantiate objects from your class. The benefit of having one constructor invoke another overloaded constructor is to avoid code duplication.