Start of Tutorial > Start of Trail > Start of Lesson | Search |
All Java classes have constructors that are used to initialize a new object of that type. A constructor has the same name as the class. For example, the name of theStack
class's constructor isStack
, the name of theRectangle
class's constructor isRectangle
, and the name of theThread
class's constructor isThread
.Stack
defines a single constructor:Java supports name overloading for constructors so that a class can have any number of constructors, all of which have the same name. Following is another constructor that could be defined bypublic Stack() { items = new Vector(10); }Stack
. This particular constructor sets the initial size of the stack according to its parameter:Both constructors share the same name,public Stack(int initialSize) { items = new Vector(initialSize); }Stack
, but they have different parameter lists. The compiler differentiates these constructors based on the number of parameters in the list and their types.Typically, a constructor uses its arguments to initialize the new object's state. When creating an object, choose the constructor whose arguments best reflect how you want to initialize the new object.
Based on the number and type of the arguments that you pass into the constructor, the compiler can determine which constructor to use. The compiler knows that when you write the following code, it should use the constructor that requires a single integer argument:
Similarly, when you write the following code, the compiler chooses the no-argument constructor or the default constructor:new Stack(10);When writing your own class, you don't have to provide constructors for it. The default constructor is automatically provided by the runtime system for any class that contains no constructors. The default provided by the runtime system doesn't do anything. So, if you want to perform some initialization, you will have to write some constructors for your class.new Stack();The constructor for the following subclass of
Thread
performs animation, sets up some default values, such as the frame speed and the number of images, and then loads the images:Note how the body of a constructor is like the body of a method; that is, it contains local variable declarations, loops, and other statements. However, one line in theclass AnimationThread extends Thread { int framesPerSecond; int numImages; Image[] images; AnimationThread(int fps, int num) { super("AnimationThread"); this.framesPerSecond = fps; this.numImages = num; this.images = new Image[numImages]; for (int i = 0; i <= numImages; i++) { . . . // Load all the images. . . . } } . . . }AnimationThread
constructor that you wouldn't see in a method is the second line:This line invokes a constructor provided by the superclass ofsuper("AnimationThread");AnimationThread
, namely,Thread
. This particularThread
constructor takes aString
that sets the name ofThread
. Often a constructor wants to take advantage of initialization code written in a class's superclass. Indeed, some classes must call their superclass constructor in order for the object to work properly. If present, the superclass constructor must be the first statement in the subclass's constructor: An object should perform the higher-level initialization first.You can specify what other objects can create instances of your class by using an access specifier in the constructors' declaration:
Constructors provide a way to initialize a new object. Initializing Instance and Class Members 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.
private
- No other class can instantiate your class. Your class may contain public class methods (sometimes called factory methods), and those methods can construct an object and return it, but no other classes can.
protected
- Only subclasses of your class can create instances of it.
public
- Any class can create an instance of your class.
- no specifier gives package access
- Only classes within the same package as your class can construct an instance of it.
Start of Tutorial > Start of Trail > Start of Lesson | Search |