A Constrcutor is the method that initialize the instance variables when the class is instantiated. Whent a class is instantiated with "new" keyword, a constructor initialize instance variables; there are several essential rules for constructor.
A constructor has NO RETURN TYPE and NO VOID keyword also.
ClassName(parameter){...}
this(): In a constructor, overloaded constructors can be called out with the method, this().
class Ex {
public Ex() {
System.out.println("the first constructor!")
}
public Ex(int x) {
this();
// this() methods that copies the default constructor in the overloaded second constructor.
System.out.println("the second constructor!")
}
}
this keyoword:
this keywords make it possible to use both the variables and parameters, which have same names, together with distingushing them in the constructors and methods: in the constructors and methods this. refers to the parameter in themselves.
Inner Class is the class that is made inside of a class, which is only used when the outer calss and inner class are related, to reduce the complexity of codes.