A constructor is a special method in java that is used to instantiate the class when creating object of that class.
every java class required to have constructor.If the constructor is not provided then Java JVM will spin a default constructor.
A default constructor is constructor that does not takes in any parameters
example bellow shows a default constructor.
If we ever wanted to create an object from the class provided above we may supply the arguments or use method called getters and setters to set values of the object.
every java class required to have constructor.If the constructor is not provided then Java JVM will spin a default constructor.
A default constructor is constructor that does not takes in any parameters
example bellow shows a default constructor.
public class Person {
private int age;
private String name;
private String city;
//default constructor
public Person() {
}
//class constructor
public Person(int age, String name, String city) {
this.age = age;
this.name = name;
this.city = city;
}
}
If we ever wanted to create an object from the class provided above we may supply the arguments or use method called getters and setters to set values of the object.
Tags:
Core java