Constructors Part 4 Tutorial

This tutorial will discuss the default constructor. When we don't declare a constructor in our source code file, the compiler will put in a default no-argument constructor for us. It is important to understand that when we declare our own constructor, the default no-argument constructor is no longer created. If we were to code a constructor in a class that is already in use in other source code files, then we will 'break' the code everywhere the object is initialized.
A default constructor looks like this:
className() {
      super();
// don't worry about what this is yet.
}



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md ConstructorFour
C:\Java>cd ConstructorFour
C:\Java\ConstructorFour>Notepad ConstructorFour.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.



class ConstructorFour {
    public static void main(String args[]) {
	
        Box b = new Box(); 
		
        if (b.setLength(10) && b.setHeight(2) && b.setWidth(5)) {
            System.out.println("The length of our box is: " + b.getLength());
            System.out.println("The height of our box is: " + b.getHeight());
            System.out.println("The width of our box is: " + b.getWidth());
            System.out.println("The volume of our box is: " + b.calculateVolume());
        } else {
            System.out.println("Unexpected value in one of the dimension arguments.");
        }
    }
}

class Box {
    private int length = 0; 
    private int height = 0; 
    private int width = 0; 

    boolean setLength (int lengthParam) {
        if (lengthParam >= 1) {
            length = lengthParam;
            return true;
        } else {
            return false;
        }
    }
    int getLength () {
        return length;
    }

    boolean setHeight (int heightParam) {
        if (heightParam >= 1) {
            height = heightParam;
            return true;
        } else {
            return false;
        }
    }
    int getHeight () {
        return height;
    }

    boolean setWidth (int widthParam) {
        if (widthParam >= 1) {
            width = widthParam;
            return true;
        } else {
            return false;
        }
    }
    int getWidth () {
        return width;
    }

    // create a method to get the volume of the box
    int calculateVolume() {
       return (length * height * width);	
    }
}

Now we will add in a constructor to Box class and recompile. We will receive an error on the Box b = new Box(); statement because once we add in a constructor, the compiler no longer creates the default constructor for us.


class ConstructorFour {
    public static void main(String args[]) {
	
        Box b = new Box(); // default no-argument constructor no longer exists, so the new operator cannot find a matching constructor anymore.
		
        if (b.setLength(10) && b.setHeight(2) && b.setWidth(5)) {
            System.out.println("The length of our box is: " + b.getLength());
            System.out.println("The height of our box is: " + b.getHeight());
            System.out.println("The width of our box is: " + b.getWidth());
            System.out.println("The volume of our box is: " + b.calculateVolume());
        } else {
            System.out.println("Unexpected value in one of the dimension arguments.");
        }
    }
}

class Box {
    private int length = 0; 
    private int height = 0; 
    private int width = 0; 

    //constructor
    Box(int lengthParam, int heightParam, int widthParam) {
        length = lengthParam;
        height = heightParam;
        width = widthParam;
    }  

    boolean setLength (int lengthParam) {
        if (lengthParam >= 1) {
            length = lengthParam;
            return true;
        } else {
            return false;
        }
    }
    int getLength () {
        return length;
    }

    boolean setHeight (int heightParam) {
        if (heightParam >= 1) {
            height = heightParam;
            return true;
        } else {
            return false;
        }
    }
    int getHeight () {
        return height;
    }

    boolean setWidth (int widthParam) {
        if (widthParam >= 1) {
            width = widthParam;
            return true;
        } else {
            return false;
        }
    }
    int getWidth () {
        return width;
    }

    // create a method to get the volume of the box
    int calculateVolume() {
       return (length * height * width);	
    }
}

So how do we fix this? We add in a no-argument default constructor to the class. We don't remove the constructor we just created, we will simply have two constructors at this point. While we are at it, we will create a second object using the new constructor structure.


class ConstructorFour {
    public static void main(String args[]) {
	
        Box b = new Box(); // It works again! yippie

        if (b.setLength(10) && b.setHeight(2) && b.setWidth(5)) {
            System.out.println("The length of our box is: " + b.getLength());
            System.out.println("The height of our box is: " + b.getHeight());
            System.out.println("The width of our box is: " + b.getWidth());
            System.out.println("The volume of our box is: " + b.calculateVolume());
        } else {
            System.out.println("Unexpected value in one of the dimension arguments.");
        }

        Box x = new Box(5, 5, 5);
        System.out.println("The volume of our other box x is: " + x.calculateVolume());
    }
}

class Box {
    private int length = 0; 
    private int height = 0; 
    private int width = 0; 

    // default constructor
    Box() {
        super(); // don't worry about what this does yet. I will cover super in future tutorials.
    }

    // constructor
    Box(int lengthParam, int heightParam, int widthParam) {
        length = lengthParam;
        height = heightParam;
        width = widthParam;
    }  

    boolean setLength (int lengthParam) {
        if (lengthParam >= 1) {
            length = lengthParam;
            return true;
        } else {
            return false;
        }
    }
    int getLength () {
        return length;
    }

    boolean setHeight (int heightParam) {
        if (heightParam >= 1) {
            height = heightParam;
            return true;
        } else {
            return false;
        }
    }
    int getHeight () {
        return height;
    }

    boolean setWidth (int widthParam) {
        if (widthParam >= 1) {
            width = widthParam;
            return true;
        } else {
            return false;
        }
    }
    int getWidth () {
        return width;
    }

    // create a method to get the volume of the box
    int calculateVolume() {
       return (length * height * width);	
    }
}

Now switch back to the command prompt (CMD) and type in javac ConstructorFour.java and press Enter.
Now type in java ConstructorFour and press Enter.


C:\Java\ConstructorFour>javac ConstructorFour.java
C:\Java\ConstructorFour>java ConstructorFour
The length of our box is: 10
The height of our box is: 2
The width of our box is: 5
The volume of our box is: 100

Final thoughts

You will want to make it a habit to always write a default no-argument constructor when you write a constructor that contains parameters.


Tutorials