CHAPTER 8: Class Declarations Previous
Previous
Java Language
Java Language
Index
Index
Next
Next

8.6 Constructor Declarations

8.6.1 Formal Parameters , 8.6.2 Constructor Signature , 8.6.3 Constructor Modifiers , 8.6.4 Throws , 8.6.5 Constructor Body , 8.6.6 Constructor Overloading , 8.6.7 Default Constructor , 8.6.8 Preventing Instantiation of a Class

A constructor is used in the creation of an object that is an instance of a class:


ConstructorDeclaration:

	ConstructorModifiersopt ConstructorDeclarator

		Throwsopt ConstructorBody

ConstructorDeclarator:

	SimpleTypeName ( FormalParameterListopt )

The SimpleTypeName in the ConstructorDeclarator must be the simple name of the class that contains the constructor declaration; otherwise a compile-time error occurs. In all other respects, the constructor declaration looks just like a method declaration that has no result type.

Here is a simple example:


class Point {
	int x, y;
	Point(int x, int y) { this.x = x; this.y = y; }
}

Constructors are invoked by class instance creation expressions (S15.8), by the newInstance method of class Class (S20.3), by the conversions and concatenations caused by the string concatenation operator + (S15.17.1), and by explicit constructor invocations from other constructors (S8.6.5). Constructors are never invoked by method invocation expressions (S15.11).

Access to constructors is governed by access modifiers (S6.6). This is useful, for example, in preventing instantiation by declaring an inaccessible constructor (S8.6.8).

Constructor declarations are not members. They are never inherited and therefore are not subject to hiding or overriding.


8.6.1 Formal Parameters

The formal parameters of a constructor are identical in structure and behavior to the formal parameters of a method (S8.4.1).


8.6.2 Constructor Signature

The signature of a constructor is identical in structure and behavior to the signature of a method (S8.4.2).


8.6.3 Constructor Modifiers


ConstructorModifiers:

	ConstructorModifier

	ConstructorModifiers ConstructorModifier

ConstructorModifier: one of

	public protected private

The access modifiers public , protected , and private are discussed in S6.6. A compile-time error occurs if the same modifier appears more than once in a constructor declaration, or if a constructor declaration has more than one of the access modifiers public , protected , and private .

Unlike methods, a constructor cannot be abstract , static , final , native , or synchronized . A constructor is not inherited, so there is no need to declare it final and an abstract constructor could never be implemented. A constructor is always invoked with respect to an object, so it makes no sense for a constructor to be static . There is no practical need for a constructor to be synchronized , because it would lock the object under construction, which is normally not made available to other threads until all constructors for the object have completed their work. The lack of native constructors is an arbitrary language design choice that makes it easy for an implementation of the Java Virtual Machine to verify that superclass constructors are always properly invoked during object creation.


8.6.4 Throws

The throws clause for a constructor is identical in structure and behavior to the throws clause for a method (S8.4.4).


8.6.5 Constructor Body

The first statement of a constructor body may be an explicit invocation of another constructor of the same class, written as this followed by a parenthesized argument list, or an explicit invocation of a constructor of the direct superclass, written as super followed by a parenthesized argument list.


ConstructorBody:

	{ ExplicitConstructorInvocationopt BlockStatementsopt }

ExplicitConstructorInvocation:

	this ( ArgumentListopt ) ;

	super ( ArgumentListopt ) ;

It is a compile-time error for a constructor to directly or indirectly invoke itself through a series of one or more explicit constructor invocations involving this .

If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object , then the constructor body is implicitly assumed by the compiler to begin with a superclass constructor invocation "super(); ", an invocation of the constructor of its direct superclass that takes no arguments.

Except for the possibility of explicit constructor invocations, the body of a constructor is like the body of a method (S8.4.5). A return statement (S14.15) may be used in the body of a constructor if it does not include an expression.

In the example:


class Point {

	int x, y;


	Point(int x, int y) { this.x = x; this.y = y; }

}


class ColoredPoint extends Point {

	static final int WHITE = 0, BLACK = 1;


	int color;


	ColoredPoint(int x, int y) {
		this(x, y, WHITE);
	}


	ColoredPoint(int x, int y, int color) {
		super(x, y);

		this.color = color;

	}

}

the first constructor of ColoredPoint invokes the second, providing an additional argument; the second constructor of ColoredPoint invokes the constructor of its superclass Point , passing along the coordinates.

An explicit constructor invocation statement may not refer to any instance variables or instance methods declared in this class or any superclass, or use this or super in any expression; otherwise, a compile-time error occurs. For example, if the first constructor of ColoredPoint in the example above were changed to:


	ColoredPoint(int x, int y) {
		this(x, y, color);
	}

then a compile-time error would occur, because an instance variable cannot be used within a superclass constructor invocation.

An invocation of the constructor of the direct superclass, whether it actually appears as an explicit constructor invocation statement or is provided automatically (S8.6.7), performs an additional implicit action after a normal return of control from the constructor: all instance variables that have initializers are initialized at that time, in the textual order in which they appear in the class declaration. An invocation of another constructor in the same class using the keyword this does not perform this additional implicit action.

S12.5 describes the creation and initialization of new class instances.


8.6.6 Constructor Overloading

Overloading of constructors is identical in behavior to overloading of methods. The overloading is resolved at compile time by each class instance creation expression (S15.8).


8.6.7 Default Constructor

If a class contains no constructor declarations, then a default constructor that takes no parameters is automatically provided:

A compile-time error occurs if a default constructor is provided by the compiler but the superclass does not have a constructor that takes no arguments.

If the class is declared public , then the default constructor is implicitly given the access modifier public (S6.6); otherwise, the default constructor has the default access implied by no access modifier. Thus, the example:


public class Point {
	int x, y;
}

is equivalent to the declaration:


public class Point {
	int x, y;
	public Point() { super(); }
}

where the default constructor is public because the class Point is public .


8.6.8 Preventing Instantiation of a Class

A class can be designed to prevent code outside the class declaration from creating instances of the class by declaring at least one constructor, to prevent the creation of an implicit constructor, and declaring all constructors to be private . A public class can likewise prevent the creation of instances outside its package by declaring at least one constructor, to prevent creation of a default constructor with public access, and declaring no constructor that is public .

Thus, in the example:


class ClassOnly {
	private ClassOnly() { }
	static String just = "only the lonely";
}

the class ClassOnly cannot be instantiated, while in the example:


package just;



public class PackageOnly {
	PackageOnly() { }
	String[] justDesserts = { "cheesecake", "ice cream" };
}

the class PackageOnly can be instantiated only within the package just , in which it is declared.

Top© 1996 Sun Microsystems, Inc. All rights reserved.