Parameterized constructor

c We have already learned about a special kind of parameterized constructor – the copy constructor.

Syntax

public class ClassName {
    ClassName(type var1, type var2, type var3) {

    }

    public static void main(String[] args) {
        ClassName obj = new ClassName(val, val, val);
    }
}

Example 1

public class Person {
    String name;

    Person(String name) {
        this.name = name;
        System.out.print("Hello " + name);
    }

    public static void main(String[] args) {
        Person obj = new Person("Michael");
    }
}
Output
Hello Michael

Example 2

public class Cube {
    int length, width, height;

    Cube(int a, int b, int c) {
        this.length = a;
        this.width = b;
        this.height = c;
    }

    int getVolume() {
        return length * width * height;
    }

    public static void main(String[] args) {
        Cube c1 = new Cube(1, 2, 3);
        Cube c2 = new Cube(2, 3, 4);
    
        System.out.println(c1.getVolume());
        System.out.print(c2.getVolume());
    }
}
Output
6
24