public class ConsDemo {
public static void main(String args[]) {
MyClass t1 = new MyClass();
MyClass t2 = new MyClass();
System.out.println(t1.num + " " + t2.num);
}
}
class Person {
var children: MutableList<Person> = mutableListOf<>()
constructor(parent: Person) {
parent.children.add(this)
}
}
class MyMainEmployee{
private int id;
private String name;
public MyMainEmployee(){
id = 0;
name = "Your-Name-Here";
}
public MyMainEmployee(String myName, int myId){
id = myId;
name = myName;
}
public MyMainEmployee(String myName){
id = 1;
name = myName;
}
public String getName(){
return name;
}
public void setName(String n){
this.name = n;
}
public void setId(int i){
this.id = i;
}
public int getId(){
return id;
}
}
public class cwh_42_constructors {
public static void main(String[] args) {
//MyMainEmployee d = new MyMainEmployee("ProgrammingWithHarry", 12);
MyMainEmployee d = new MyMainEmployee();
//d.setName("Learn java");
//d.setId(34);
System.out.println(d.getId());
System.out.println(d.getName());
}
}
Constructor are a type of method
* Share the name of class
* Called when you Instantiate a Class
Default Constructor accepts no Arguments
class Ball {
constructor(w, h) {
this.width = 100;
this.height = 100;
}
}