Java Tutorial: Constructers

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 “constructer”. (often spelled “constructor”) The purpose of constructers 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 constructers in a class, distinguished by their parameters. When the class is initialized, Java will call the right constructer with matching arguments and type.

Example:

// a class with 3 constructers, 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 constructers's parameters may be other objects.

Reference: Java Tutorial: constructors↗.

Reference: Java Lang Spec: classes↗.

Java Technicality: Default Constructers

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

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

So, for example, if you have a class B that has a constructer “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 constructer.
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 constructer so it did not automatically create the
no-parameter and do-nothing constructer.

remove the user defined constructer, and the code compiles.
 */
class B {
    int x;  
    B (int n) {
        x=n;
        System.out.println("constructer '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 constructer that takes some argument, you should also write one without any argument, even if it will be doing nothing.


Note that constructers do not have a return type declaration. See constructer and void.

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


Page created: 2005-01.
© 2005 by Xah Lee.
Xah Signet