Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

7.5 Constructors

A constructor is a way of creating an object. The most common way to do this is using the new keyword, as we have done a number of times already:

Type ObjectName =
new Type(arguments);

When you write this statement, the runtime determines if there is enough memory space to create your object and the data that it will hold. If there is enough, the object is created and set to its initial state. If there is not enough space, then the runtime looks for space it can reclaim and runs the garbage collector to reclaim it. The garbage collector destroys items with no references, as they could no longer be necessary to a program's life. If there is still not enough free space, an OutOfMemoryError exception is thrown.

Once an object is created, its variables must be initialized. That is the purpose of constructors. For example, given an Employee class, we could perhaps create a new Employee like this:

Employee anEmployee = new Employee("Bill");

The class type is declared, a name is given to the instance of the object, and new is invoked to create a new object of type Employee. Using the new keyword tells the class to call the constructor whose argument list matches the given argument list. Because we're passing one argument—a string representing the employee's name—we need to have one constructor in our class definition that accepts a sole string as an argument. If we don't, the Java Virtual Machine will throw a NoClassDefFoundError exception.

7.5.1 Overloading Constructors

Often, classes will have more than one constructor. An object can be created with a different initial depending on which constructor is used. The type and number of arguments determines which constructor will be used to create the object. Defining multiple constructors with the same name but different parameter lists is called overloading. There are many classes in the API with overloaded constructors. The String class defines 11 different string constructors. Here are a few of them, all of which create a string:

String() // one
String(bytes[] bytes) // two
String(char[] value) // three


Defining multiple methods or constructors with the same name but different parameter lists is called overloading.

7.5.2 No-arg and Default Constructors

No-arg constructors are constructors that do not accept arguments. You can explicitly write a no-arg constructor in your code. If you do not, then Java writes in a no-arg constructor that does nothing. That constructor is called the default constructor.

The default constructor is provided only in the event that no other constructors exist.

7.5.3 Static Constructors

Constructors cannot be declared static, and so Java gives us a way to perform static initialization. To declare a static constructor, you use the keyword static followed by the body of the method you're defining.


You can declare multiple static constructors, but the Java compiler will combine all of them into one method. The reason for this is to ensure that the static constructors are compiled in the same order in which they are written in the source code.

The following program illustrates the fact that static constructors get executed immediately upon the class loading—even before the main() method. You can use this to advantage in program design—perhaps to display a tip to your users on start up, like in Forte and IntelliJ.

 * StaticConstructorTest.java
 * Created on June 8, 2002, 3:07 PM

package chp7;

 * @version 1.0
public class StaticConstructorTest {
  static String s;
  static int stringLength;
  // first part of static constructor
  static {
    s = "I am s in the FIRST constructor";

  public static void main (String [] arg) {
    System.out.println("Now here's the MAIN method.");
  // second part of constructor will execute before main
  static {
    s = "I am s in the SECOND constructor.";
  • + Share This
  • 🔖 Save To Your Account