The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Classes and Inheritance

Providing Constructors for Your Classes

All classes have at least one constructor. A constructor is used to initialize a new object of that type and looks like a method with the same name as the class but without a return type. For example, Stack has one constructor:
public Stack() {
    items = new ArrayList<Object>();
}
A constructor is called by the new operator, which automatically returns the newly created object. Though our example doesn't have a second constructor, it could have another that creates a list with an initial capacity:
public Stack(int size) {
    items = new ArrayList<Object>(size);
}
Both constructors could have been declared in Stack because they have different argument lists. As with methods, the Java platform differentiates constructors on the basis of the number of arguments in the list and their types. You cannot write two constructors that have the same number and type of arguments for the same class, because the platform would not be able to tell them apart. Doing so causes a compile-time error.

When writing a class, you should provide it with whatever constructors make sense for that class. Recall the Rectangle class discussed in the The Life Cycle of an Object (in the Learning the Java Language trail) section. That class contains three constructors that allow the user to initialize a new rectangle object in a variety of ways. You don't have to provide any constructors for your class if that's what makes sense. The compiler automatically provides a no-argument, default constructor for any class without constructors. The default constructor will call a no-argument constructor of the superclass. The compiler will complain if the superclass doesn't have a no-argument constructor. This can lead to some surprising compiler errors. For example, save these two class declarations in a file called Test.java and see what the compiler says:

class NoNoArg {
    NoNoArg(Object argument) {}
}

class Default extends NoNoArg {}

You can use one of the following access specifiers in a constructor's declaration to control what other classes can call the constructor:

private
Only this class can use this constructor. Making a constructor private, in essence, makes the class final — the class can't be subclassed, except from within. If all constructors within a class are private, the class might contain public class methods (called factory methods) that create and initialize an instance of this class. Other classes can use the factory methods to create an instance of this class.
protected
Subclasses of this class and classes in the same package can use this constructor.
public
Any class can use this constructor.
no specifier
If no access specifier is provided, the constructor has the default access (sometimes called package private). Only classes within the same package as this class can use this constructor.

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.