Constructors

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

        ClassName objVar = new ClassName( params );
                               ^^^^^^^^^^^^^^^^^^^
    	             invokes a constructor with matching signature 

  • 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 through the use the new operator:

         new ClassName(...) will invoke a constructor method
        

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 will define a regular method (= action) that can be invoke on a Circle object

  • The correct syntax is:

        public Circle(double newRadius)  // Without a return type
        {
            .... 
        } 
    

Constructors can be overloaded

Just 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 object that is created with the new operator must be initialized.

  • Therefore, every class in a Java program will obey the following rules about constructors:

    • Every class must have at least one constructor

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

          className()     // The default constructor
          {
          }

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

    • The "default constructor"

I will show you examples to illustrate the rules next

Rules on constructors and the default constructor

Consider a Circle class that contains 2 constructors:

  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/10-classes/05-rules-constr/Circle.java + Demo.java     --    Compile with no error

Rules on constructors and the default constructor

Suppose we delete the constructor Circle( ):

  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 }

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

DEMO: demo/10-classes/05-rules-constr     --    delete Circle( ) and show compile error

Rules on constructors and the default constructor

Suppose we delete both constructors:

  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!! - no error 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/10-classes/05-rules-constr (delete Circle(double))