Constructors are used in the creation of new objects. Constructors are defined in class bodies with a syntax similar to that of method declaration. A constructor is named with the class name and does not have a specified return type. A constructor has a parameter list, which can be empty, and a body consisting of a block. For instance,
public class Rectangle { public Rectangle (int width, int length) { this.width = width; this.length = length; } ... } public class Circle { public Circle () { this.radius = 1.0; } ... }
A class may have more than one constructor. Like overloaded methods, overloaded constructors must differ in number and/or type of parameters. (That is, a class cannot have two constructors with the same signature.)
Constructors are not technically members of a class, and are not inherited by subclasses.
A new instance of a class is created by evaluation of a
class instance creation expression. The simplest form of class instance creation expression consists of the keyword new
followed by the name of the class to be instantiated, followed by an argument list. For example,
new Rectangle(10,20)
The arguments must match the parameters of a constructor for the class in number and type, in the same way that the arguments in a method invocation must match the parameters of the method.
Evaluating a class instance creation expression results in creation of a new object and execution of a constructor to initialize the newly created object. A reference to the object is returned as the value of the expression.
Every constructor (except for class Object
) begins with an implicit or explicit invocation of another constructor of the same class or of a constructor of the immediate superclass. The class Object
is the immediate superclass of the classes Rectangle
and Circle
shown above. The constructors illustrated begin with implicit invocations of the parameterless constructor of the class Object
.
A superclass constructor can be explicitly invoked with the keyword super
. Thus the above constructors are equivalent to
public Rectangle (int width, int length) { super(); this.width = width; this.length = length; } public Circle () { super(); this.radius = 1.0; }
If the immediate superclass does not have a parameterless constructor, a superclass constructor must be explicitly invoked. For example, suppose the class Circle
has one constructor, specified as
public Circle (double radius) { ... }
and the class ColoredCircle
extends Circle
:
public class ColoredCircle extends Circle { public ColoredCircle (double radius, Color color) { ...
If the ColoredCircle
constructor does not explicitly invoke the Circle
constructor, the invocation
super();
will be applied by default. But Circle
does not have a parameterless constructor. The ColoredCircle
constructor must therefore explicitly invoke the constructor of its parent class:
public ColoredCircle (double radius, Color color) { super(radius); ...
A constructor may explicitly invoke another constructor of the same class with the keyword this
. For example, the class Circle
might contain the following two constructors:
public Circle (double radius) { this.radius = radius; } public Circle () { this(1.0); }
The parameterless constructor invokes the other constructor with an argument value of 1.0.
Use of the keywords super
and this
to invoke constructors is only permitted as the first statement of a constructor. For example, the following is not legal:
public ColoredCircle (double radius, Color color) { this.color = color; super(radius); // this must be the first statement! }
Every class, even an abstract class, has at least one constructor. If a class does not declare a constructor, the class is provided with the default constructor. The default constructor has no parameters, and has a body that consists of the statement
super();
The access modifier of the default constructor is the same as that of the class. For instance, if the class Sorter
were defined without a constructor,
public class Sorter { public void sort (List list) { ... } }
it would be provided with the default constructor
public Sorter () { super(); }
To create an instance of an inner class, the class instance creation expression can be qualified. For example, suppose Traverser
is an inner class of Tree
:
public class Tree { ... public class Traverser { public Traverser () { this.start = Tree.this.root; } ... private Node start; } ... private Node root; }
To create an instance of Tree.Traverser
, the keyword new
is prefixed with a reference to a Tree
:
Tree tree = new Tree(); Tree.Traverser index = tree.new Traverser();
Note that to create an instance of a static nested class, the qualified class name can be used. For instance, if the class Tree.Traverser
were declared static
:
public class Tree { ... public static class Traverser { public Traverser (Tree t) { this.start = t.root; } ... }
an instance could be created as follows:
Tree tree = new Tree(); Tree.Traverser index = new Tree.Traverser(tree);
An anonymous class is a nameless class that is simultaneously declared and instantiated in a single class instance creation expression. The class instance creation expression for an anonymous class includes the body of the class.
An anonymous class has a "parent" that is either a class or an interface. When declaring and instantiating an anonymous class, the name of the parent is used in the creation expression. The syntax is
new parent(argumentList) classBody
or for a subclass of an inner class
prefix.new parent(argumentList) classBody
For example, the following expression declares an anonymous class that implements the interface ActionListener
, and instantiates the class:
new ActionListener() { public void actionPerformed (ActionEvent e) { System.out.println(e.paramString()); } }
The interface ActionListener
specifies the abstract method actionPerformed
:
public interface ActionListener { ... void actionPerformed(ActionEvent e); }
Thus the anonymous subclass that implements the interface must implement this method.