Constructors

  • Constructors are special methods in a class that is only invoked when an object is created using the new operator:

        ClassName objVar = new ClassName(...); 

  • Constructors have 3 special properties:

    1. A constructor must have the same name as the class itself

    2. Constructors do not have a return type - not even void !

    3. Constructors cannot be invoked like an ordinary method.

        • A constructor is only invoked when you use the new operator to create an object

Common mistake when defining a constructor

  • A very common mistake students make when defining a constructor:

    public class Circle
    {
        public void Circle(double newRadius) 
        {
            .... 
        } 
    
        ...
    } 

    This syntax does not define a constructor !!

    (This syntax defines a regular method (= action) that you can invoke on a Circle object)

Constructors can be overloaded

Like regular methods, constructors can be overloaded
(I.e., multiple constructors can be defined with different signatures)

public class Circle 
{
    public double radius = 1;       /** The radius of this circle */

    public Circle() { }             /** Called when you use: new Circle() */

    public Circle(double newRadius) /** Called when you use: new Circle(num) */
    {
        radius = newRadius;
    }

    public double getArea()      /** Return the area of this circle */
    {
        return 3.14159 * radius * radius;
    }

    public void setRadius(double newRadius) /** Set new radius for this circle */
    {
       radius = newRadius;
    }
} 

Rules on constructors and the default constructor

  • Every class must have at least one constructor

  • If a class does not have any constructor, then the Java compiler will automatically insert this constructor for you:

          className() { }                       

    The constructor that is inserted by the Java compiler is called:   the "default constructor"

  • Note:

      • The Java compiler will not insert the default constructor if there is a constructor defined in the class

I will show you examples to illustrate the rules next

Rules on constructors and the default constructor

The Circle class has 2 constructors defined:

public class Circle 
{
    double radius = 1;       /** The radius of this circle */

    Circle() { }             /** Constructor 1 for a circle object */

    Circle(double newRadius) /** Constructor 2 for a circle object */
    {
        radius = newRadius;
    }
}

public static void main(String[] args) { Circle a = new Circle(); // Invokes constructor 1 Circle b = new Circle(2); // Invokes constructor 2 }

The Circle class has at least 1 constructor, so the Java compiler will not insert the default constructor

DEMO: demo/03-classes/05-rules-constr

Rules on constructors and the default constructor

The Circle class now has 1 constructor defined:

public class Circle 
{
    double radius = 1;       /** The radius of this circle */



    Circle(double newRadius) /** Constructor   for a circle object */
    {
        radius = newRadius;
    }
}

public static void main(String[] args) { Circle a = new Circle(); // Compile error (constructor not found) Circle b = new Circle(2); // Invokes constructor }

The Circle class has at least 1 constructor, so the Java compiler will not insert the default constructor

DEMO: demo/03-classes/05-rules-constr (delete Circle())

Rules on constructors and the default constructor

The Circle class now has no constructors defined:

public class Circle 
{
    double radius = 1;       /** The radius of this circle */



  
    
       
   
}

public static void main(String[] args) { Circle a = new Circle(); // Invokes the default constructor !! Circle b = new Circle(2); // Compile error (constructor not found) }

The Circle class has no constructors, so the Java compiler will insert the default constructor

DEMO: demo/03-classes/05-rules-constr (delete Circle(double))