If you enjoyed this site, please consider donating $3. Any amount is appreciated. Thanks!

Java Tutorial: Constructors

Xah Lee, 2005-02

In Java, if you want a piece of code to run when your class is called for the first time, just define a method in that class with the same name as that class and omit its return type declaration.

Such a method is called a “constructor”. The purpose of constructors are basically to set some variables when the object is created for the first time. This practice is called “intialization”.

// Example of defining a consturcter and using it to create a object
class c2 {
    c2 (int n) { System.out.println("i'll do this and that!");}
}

class c1 {
    public static void main(String[] arg) {
        c2 x = new c2(3);
    }
}

There can be more than one constructors in a class, distinguished by their parameters. When the class is initialized, Java will call the right constructor with matching arguments and type.

Example:

// a class with 3 constructors, differing by their parameters
class t2 {
    t2 () { System.out.println("empty arg called!");}
    t2 (int n) { System.out.println("int one called!");}
    t2 (double n) { System.out.println("double one called!");}
}

class t1 {
    public static void main(String[] arg) {
// creating 3 objects of t2.
// when each object are created, Java automatically calls the right constructor
        t2 x1 = new t2();
        t2 x2 = new t2(3);
        t2 x3 = new t2(3.0);
    }
}

The above are simple examples. In general, your constructors's parameters may be other objects.

Java Tutorial: constructors

Java Lang Spec: classes

Java Technicality: Default Constructors

Java technicality: every class has a constructor even though none are defined. When a class does not define any constructor, the Java compiler actually automatically creates a do-nothing constructor with no argument. So, if a class B does not define any constructors and when a object of B is created “B b = new B()”, internally Java calls the default constructor B(), which does nothing.

This internal stuff is important to know. Because for example, once a constructor is defined by the programer, Java no longer internally creates this default no-argument constructor.

So, for example, if you have a class B that has a constructor “B (int n)”, you cannot create a object of B by “new B()” because you didn't define it and Java didn't create it automatically. It is a compilation error.

/*
This example shows a class B with a user defined constructor.
However, the when a obj B is created by “new B()”, it creates a
compiler error because the Java compiler saw the existance of a user
defined constructor so it did not automatically create the
no-parameter and do-nothing constructor.

remove the user defined constructor, and the code compiles.
 */
class B {
    int x;  
    B (int n) {
        x=n;
        System.out.println("constructor 'B (int n)' called!");
    }
}

public class cons { 
    public static void main(String[] args) {B b = new B();}
}

The upshot of this “Java internal business” is that when you write a constructor that takes some argument, you should also write one without any argument, even if it will be doing nothing.


Note that constructors do not have a return type declaration. See constructor and void.

Note also, constructors are not inherited. See inheritance with constructors.

2005-01
© 2005 by Xah Lee.