Java - Class - Constructors

Java

In this tutorial we will learn about constructor of a class in Java programming language.

So far we have learned how to create class, how to add member variables inside a class and how to add methods in a class.

Now lets talk about constructor.

What is a constructor?

A constructor is a special method inside a class that has the exact name like the class and it helps to initialise the object of the class.

Constructor has no return type, not even void. And the implicit return type of the constructor is the class type itself.

If we don't explicitly add a constructor to a class then Java compiler adds a default constructor to the class.

Constructor syntax

class NameOfTheClass {

  // some member variables
  
  // constructor
  NameOfTheClass() {
    // some code goes here...
  }

  // some methods of the class
}

In the following example we are creating a Happy class having a constructor that prints a message when the object of the class is created.

class Happy {

  // constructor
  Happy() {
    System.out.println("Hello from the Happy constructor.");
  }
}

// Our main class
class Example {
  public static void main(String[] args) {
    Happy obj = new Happy();
  }
}

Output:

Hello from the Happy constructor.

Constructor with parameters

class NameOfTheClass {
  
  // constructor with parameters
  NameOfTheClass(list_of_params) {
    // some code...
  }
}

In the following example we modified the above Happy class to take parameter.

class Happy {

  // constructor
  Happy(boolean isHappy) {
    if (isHappy == true) {
      System.out.println("I am happy :)");
    } else {
      System.out.println("I am sad :(");
    }
  }
}

// Our main class
class Example {
  public static void main(String[] args) {

    // code is not working...
    boolean amIHappy = false;
    System.out.println("First run: when code is not working...");
    Happy obj = new Happy(amIHappy);

    // after sometime... code is working...
    System.out.println("After sometime second run: when code is working...");
    amIHappy = true;
    Happy obj2 = new Happy(amIHappy);
  }
}

Output:

First run: when code is not working...
I am sad :(
After sometime second run: when code is working...
I am happy :)

So, in the above code we have created the constructor Happy that takes an argument.

We are then instantiating an object of the Happy class and passing an arugment amIHappy of boolean type.

Example

In the following example we will modify the PackagingBox class that we have been working on in the previous tutorials. We will be initialising the object using constructor.

/**
 * The PackagingBox Class
 */
class PackagingBox {

  // member variables
  private double length;
  private double breadth;
  private double height;
  public double volume;
  double weight;
  double price;
  
  // constructor
  PackagingBox(double length, double breadth, double height, double weight, double price) {
    
    // we are setting the length, breadth, height, weight, price of the box
    this.length = length;
    this.breadth = breadth;
    this.height = height;
    this.volume = volume;
    this.weight = weight;
    this.price = price;
    
    // compute the volume
    this.computeVolume();
  }

  // methods

  //---- get and set length

  public void setLength(double length) {
    this.length = length;
  }
  
  public double getLength() {
    return this.length;
  }

  //---- get and set breadth
  
  public void setBreadth(double breadth) {
    this.breadth = breadth;
  }
  
  public double getBreadth() {
    return this.breadth;
  }

  //---- get and set height
  
  public void setHeight(double height) {
    this.height = height;
  }
  
  public double getHeight() {
    return this.height;
  }

  //---- get and set weight
  
  public void setWeight(double weight) {
    this.weight = weight;
  }
  
  public double getWeight() {
    return this.weight;
  }

  //---- get and set price
  
  public void setPrice(double price) {
    this.price = price;
  }
  
  public double getPrice() {
    return this.price;
  }

  //---- compute and get volume

  public void computeVolume() {
    this.volume = this.length * this.breadth * this.height;
  }

  public double getVolume() {
    return this.volume;
  }
}


/**
 * The main class.
 */
class ObjectExample {
  
  public static void main(String[] args) {

    // creating an object of the class
    PackagingBox myBox = new PackagingBox(10, 20, 30, 120, 299);

    // get the values
    System.out.println("Dimension of the box:");
    System.out.println("Length: " + myBox.getLength());
    System.out.println("Breadth: " + myBox.getBreadth());
    System.out.println("Height: " + myBox.getHeight());

    System.out.println("Weight, Volume and Price of the box:");
    System.out.println("Weight: " + myBox.getWeight());
    System.out.println("Volume: " + myBox.getVolume());
    System.out.println("Price: " + myBox.getPrice());
  }
}

Output:

Dimension of the box:
Length: 10.0
Breadth: 20.0
Height: 30.0
Weight, Volume and Price of the box:
Weight: 120.0
Volume: 6000.0
Price: 299.0

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT