c We have already learned about a special kind of parameterized constructor – the copy constructor.
public class ClassName {
ClassName(type var1, type var2, type var3) {
}
public static void main(String[] args) {
ClassName obj = new ClassName(val, val, val);
}
}
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");
}
}
Hello Michael
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());
}
}
6
24