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 has the same name as the class. For example, the name of the Stack class's single constructor is Stack:
public Stack(int size) {
    items = new Object[size];
}
This particular constructor sets the initial size of the stack according to its integer argument. A constructor is not a method, so it has no return type. A constructor is called by the new operator, which automatically returns the newly created object. You cannot use the return statement in a constructor. Another Stack constructor creates a fixed size stack:
public Stack() {
    items = new Object[10];
}
Both constructors share the same name, Stack, but 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 section The Life Cycle of an Object (in the Learning the Java Language trail). That class contains four 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 runtime system automatically provides a no-argument, default constructor for any class that contains no constructors. The default provided by the runtime system doesn't do anything.

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. 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
Gives package access. Only classes within the same package as this class can use this constructor.
Constructors provide a way to initialize a new object. The section Initializing Instance and Class Members (in the Learning the Java Language trail) describes other ways you can provide for the initialization of your class and a new object created from the class. That section also discusses when and why you would use each technique.

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.