1.5 Constructors / Nested ClassesHomepage  « Java5 Certification « 1.5 Constructors / Nested Classes

In this lesson we look constructors and how they are instantiated and invoked. We then look at nested classes and how we instantiate them.

Lets take a look at the points outlined at the Oracle Website for this part of the certification.

  • Section 1: Declarations, Initialization and Scoping

    • Given a set of classes and superclasses, develop constructors for one or more of the classes. Given a class declaration, determine if a default constructor will be created, and if so, determine the behavior of that constructor. Given a nested or non-nested class listing, write code to instantiate the class.

Constructorsgo to top of page Top

Constructors allow us to instantiate our objects via declaration, assignment and creation.

object creation
  1. Declaration - Here. we declare a reference variable named moggy of type Cat and the JVM allocates space for it.
  2. Creation - Tells the JVM to allocate space on The Heap for a new Cat object.
  3. Assignment - Assign the new Cat object to the reference variable moggy.

Access Modifiers

The table below shows the types of access available in Java for constructors.

Access modifier Description
publicA constructor may be declared with the public access modifier, and if it is the constructor is accessible to all other classes everywhere, assuming the class it resides in is accessible.
protectedA constructor may be declared with the protected access modifier, and if so, it is only accessible to the package the implementation is in.

See the Packages lesson for more information on packaging.
no modifier
package-private /
(the default)
If a constructor has no explicit access modifier, it is only accessible to the package the implementation is in.

See the Packages lesson for more information on packaging.
privateA constructor may be declared with the private access modifier, and if it is the constructor can only be constructed from within its own class.

Constructor Checklist

  • A constructor runs when we code the new() operator followed by a class name.
  • Constructors must have the same name as the class and no return type.
  • Constructors are used to initialize the instance variables (object state) of the object instance being constructed.
  • If you don't code a constructor in your class the compiler will put in a default no arguments constructor.
  • If you do code a constructor in your class, the compiler will NOT put in a default no arguments constructor, you will have to code it yourself.
  • We can have more than one constructor in a class and the constructors are then known as overloaded constructors.
  • When using overloaded constructors in a class, each constructor must have different argument lists so the compiler knows which constructor to use to construct our objects. Having the same argument types is fine as long as the order differs.
  • You can refer to any member of the current object from within a non-static method or constructor by using the this() keyword.
  • You can invoke one constructor from another constructor within the same class by calling this() and doing so is known as explicit constructor invocation and is the only way to invoke a constructor.
  • If we decide to use this() it must be the first statement within our constructor or we get a compiler error. This means we can't use this() and super() together.
  • We can use super() to invoke a superclass constructor and if we don't supply this explicitly, then the compiler inserts a no-args super() for us as the first statement in the constructor if we haven't used the this() keyword.
  • When explicitly coding super() we can supply arguments to invoke a constructor in the superclass matching the signature of our call.
  • When explicitly coding super() it must be the first statement within the constructor or we get a compiler error. This means we can't use super() and this() together.
  • When explicitly coding super() only methods and static variables can be used within the call.
  • Interfaces do not have constructors as they are not part of a particular classes inheritance tree.
  • Abstract classes do have constructors, although we can't code them, as they are part of a classes inheritance tree and so are called via super() on concrete subclass instantiation.

  Some code showing constructor usage
public class A { 
    public static void main(String args[]) { 
        A a = new A(); // OK, compiler puts in a default no arguments constructor so we can instantiate

public class B { 
    B() { } // OK, we code our own default no arguments constructor

public class C { 
    void C() { } // OK, this is a method with the same name as the class as it has a return type
    public static void main(String args[]) { 
        C c = new C();
public class D { 
    D() { }
    D(int i) { } // OK, overloaded constructor
    public static void main(String args[]) { 
        D d = new D();
        D d2 = new D(5);

public class E { 
    int i;    
    String s;

    E(int i) {
        this(i, "unknown"); // Invoke one constructor from another constructor within the same class
    E(int i, String s) {
        this.i = i;
        this.s = s;
    public static void main(String args[]) { 
        E e = new E(5);

  Following code will fail as compiler inserts a no arguments constructor for us which invokes 
  super() and there isn't a no arguments constructor in superclass
public class F extends E { 

public class G extends E { 
    G(int i) { // here we invoke existing super constructor so works fine 
    public static void main(String args[]) { 
        G g = new G(5);

Nested Classesgo to top of page Top

A nested class is only known to the enclosing class and shares its scope. This means that non-static nested classes have access to all the members and variables of the outer class. Conversely the outer class knows nothing of the internal working of the nested class. The benefits that all nested classes receive are:

  • When a class has a specific purpose that is only relevant to one other class it makes sense to put the helper class within the class that uses it and we can use a nested class for this purpose.
  • Because nested classes have access to all the outer classes members, including members with the private access modifier, it gives us a way to keep outer class members private while still being able to access them, thus increasing encapsulation.
  • Having nested classes allows us to have inner classes close to the top-level classes that enclose them, making code easier to understand and maintain.

Nested classes can be static, which are known as static member classes, or non-static, which are known as inner classes. The lesson on Nested Classes covers instantiation of all types of nested class so we will just cover the guidelines for each type here:

Static Member Classes

A static member class is associated with its outer class in the same way that static members of the outer class would be. This means that a static member class cannot refer to non-static variables and methods defined within the outer class and can only interact with them through an object reference.

Behaviourally, static member classes act like any other top-level class and essentially are top-level classes that have been nested in another top-level class to facilitate packaging, or are associated with the outer class but it makes no sense to attach them to an instance of that class.

Non-static Member Classes

This type of inner class is a member of the outer class just like instance variables and methods are and as such the same modifiers and rules apply as to amy member. We can instantiate a non-static member class as part of the outer class state or seperately when you don't want all outer instances to have an inner instance.

  • Using non-static member classes is very useful when we require a class that doesn't pass the IS-A test but is intrinsicly associated with its enclosing class.
  • A non-static member class is a member of a class just like any other member and as such can have the same modifers applied in its class declaration: these being abstract, final, private, protected, public, static and strictfp.
    • Of course using the static keyword in the class declaration means we have turned the non-static member class into a static member class and so no longer have access to any instance variables and methods.
  • If the class is required by other classes as well, then the class would be better suited as a standard top-level class instead.

Local Inner Classes

Local inner classes can be declared anywhere a local variable can be declared and have the same Method Scope. If you don't require the local inner class to be attached to an instance of the enclosing class, then the nested class should be declared in a static context. Local inner classes cannot contain static members and for readability the coding should be kept to a minimum.

  • The only valid modifers you can apply to a local inner class declaration are final or abstract.
  • Local inner classes can only be instantiated from within the method they are declared in.
  • When instantiating a local inner class, the instantiation code must come after the local inner class declaration.
  • The local inner class can only use final local variables defined within the method it resides in, outside those defined within itself and the outer instance.
  • When creating local inner classes and the code is quite robust, or the code needs to be accessible from more than one method, it is better to make a non-static member class.

Anonymous Inner Classes

The final type of inner classes we can use are anonymous inner classes which are different syntactically from anything else in Java and come with a lot of constraints:

  • Anonymous inner classes as the terminology implies have no name.
  • You can't execute the instanceof test against anonymous inner classes or any process that requires the name of the class.
  • Anonymous inner classes are not members of their enclosing class; in fact they are both declared and instantiated at the point of use.
  • Anonymous inner classes cannot contain any static members
  • Anonymous inner classes can be coded anywhere where an expression is legal, so keep the code to a minimum to maintain readability.
  • Anonymous inner classes can't be declared to extend a class and implement an interface
  • Anonymous inner classes can't implement multiple interfaces.
  • Anonymous inner classes can only invoke members inherited from the supertype

Related Java5 Tutorials

Objects & Classes - Reference Variables - The new Operator
Objects & Classes - Constructors
Inheritance Concepts -Superclass Constructors
Nested Classes

<<  1.4 Overriding & Overloading                    2: Flow Control  >>

go to home page Java5 Tutor Homepage go to home page Top

All the Java5 Certification Declarations, Initialization and Scoping lessons are listed below. Click a link to go to that lesson.

1: Declarations

1.1 Classes, Packages & Imports

1.2 Interfaces / Abstract Classes

1.3 Variables & Scope

1.4 Overriding & Overloading

1.5 Constructors / Nested Classes

Nested Classes