Incremental Java
Constructors, Part 1

Creating Objects

In our introduction to objects, we talked about String objects. Recall that objects had to be created, or in Java lingo, they have to be constructed.

However, Strings are constructed uniquely compared to all other objects in Java.

For all other objects, the class must define a special method called a constructor.

The purpose of a constructor is to provide initial values to the instance variables.

Let's write out the method signature for two constructors.

   public Rectangle() ;
   public Rectangle( int initWidth, int initHeight ) ;
Most classes have at least one constructor, otherwise, you wouldn't be able to create an object. In this case, we have two.

Perhaps the first thing you notice is that the name of the method is the name of the class. Constructors are named after the class. If you see a class name, you know it's a constructor.

The second thing you may notice is a lack of a return type. That's because the name of the class is the return type. We are constructing a Rectangle.

A constructor can only be called once, to create the object. Once the object is created, the constructor is no longer a valid method to call on that object. However, you can call constructors to create other objects. Once the object is created, you use the other methods listed.

A Closer Look at the Constructors

The first constructor, which has an empty parameter list, is often called the default constructor. Many classes have default constructors. I prefer the name empty constructors, but this is not terribly common.

We assume this constructor initializes the Rectangle object to have height and width of 0. It's common to initialize to 0 when you have no other information available.

The second constructor doesn't have a special name. It has two parameters, initWidth and initHeight. Like other methods, you can have parameters for constructors. These are often used to initialized the instance variables. The second constructor initialize width with initWidth and height with initHeight.

Constructing Rectangles

Let's construct a Rectangle object.
public static void main( String [] args )
{
   Rectangle rect1 = new Rectangle() ;
}
To call a constructor, we must use the keyword new, then the name of the class, then provide it an argument list. In this example, the argument list is empty, i.e., it is ().

Notice I used argument list. When we are in the class definition writing method signatures, we had a parameter list. When it's being called or used, it's called an argument list. We'll see why there's a different name in the next example, using the non-default constructor.

public static void main( String [] args )
{
   Rectangle rect2 = new Rectangle( 3, 5 ) ;
}
This time, we use the second constructor. The argument list is ( 3, 5 ), and appears just after the class name. For the second constructor, the argument lists indicates we want an initial width of 3 and an initial height of 5.

The parameter list contains parameters which are a list of types and parameter names. The argument list are lists of expressions. There's one argument per parameter, and the argument type must match the parameter type.

For example, the second constructor has two int parameters. So the argument list must be two int expressions.

If the constructor had an int parameter followed by a boolean parameter, we would have to provide a int expression, followed by a boolean expression, in that order.

Notice that it can be an expression. The following would be valid.

public static void main( String [] args )
{
   int x = 2 ;
   Rectangle rect2 = new Rectangle( x + 1, (2 * y) + 1 ) ;
}
To call the constructor properly,