J2EEOnline J2EEOnline

Classes/Objects  «Prev  Next»
Lesson 5Constructors
Objective Explain the syntax of class constructors.


Constructors are used to create instances of classes. Their syntax follows:

modifiers ConstructorName(arguments) throws Exception {
 // Constructor body

The modifiers, arguments, and throws clause are optional. The modifiers may be
  1. public,
  2. protected,
  3. private, or
  4. none (package access).
The arguments and throws clause are defined in the same way as with methods (see Module 5).
Constructors are not inherited from their superclasses; they must be defined for each class. There is one exception: if no constructors are defined for a class, then the compiler defines a default, parameterless constructor. If the class is declared as public, then the default constructor is public. Otherwise, it has no modifiers.

this() and super()

Sometimes you may want to define one constructor in terms of another. Consider the following example:
class MyClass {
     int i;
     String s;
     public MyClass(int i, String s) {
      this.i = i;
      this.s = s;
     public MyClass() {

The first MyClass() constructor sets the i and s field variables to the values of the i and s parameters. The this identifier refers to the current instance of MyClass being created. The second MyClass() constructor creates a MyClass object using the first constructor. The this(10,"none") causes the first constructor to be invoked with the arguments of 10 and "none."
If this() is used, it must be the first statement in a constructor.
The use of super() is similar to this() except that it invokes a constructor of the class's superclass. If super() is used, it must also be the first statement in a constructor. If neither this() nor super() is supplied, an implicit super() (with no arguments) is supplied by the compiler.
This causes the superclass portion of an object to be created before that of the class itself.


Constructors are used to initialize an object. Whenever an object is created, a constructor executes. A default constructor is the one that has no arguments and is provided automatically for all classes. This constructor will initialize all instance variables to default values. However, if the developer provides a constructor, the compiler's default constructor is no longer added and the developer will need to explicitly add a default constructor. It is a good practice to always have a default, no-argument constructor.

Which of the following statements are true?
  1. Non-static nested classes must have either default or public accessibility.
  2. Non-static nested classes cannot contain static members.
  3. Methods in all nested classes can be declared static.
  4. All nested classes can be declared static.
  5. A static inner class can contain a non-static inner class.

Answer: e
a. Non-static nested classes, unlike normal package classes, can have any type of accessibility.
b. They can if the static variable is also made final.
c. Not in non-static inner classes.
d. Local (i.e. inside a method) nested classes cannot be declared static.
Note: As per http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html : Nested classes are divided into two categories: 1) static and 2) non-static. Nested classes that are declared static are simply called static nested classes. Non-static nested classes are called inner classes. Further, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience. As opposed to top-level classes (whether nested or not), inner classes can declare a static member only if it is a constant (i.e. final). To create a class variable for an inner class, the programmer must place the desired variable in an enclosing class. It is helpful at this point to abuse the terminology somewhat, and say, loosely, that the static keyword always marks a "top-level" construct (variable, method or class), which is never subject to an enclosing instance. This shows why an inner class cannot declare a static member, because the entire body of the inner class is in the scope of one or more enclosing instances.
Only top-level nested classes can be declared static. Declaring a class static only means that instances of the class are created without having an outer instance.