A constructor is a special type of method that is used to create an instance of a class. It is called when an object of a class is created and has the same name as the class. A constructor does not have a return type and is used to initialize the state of an object.

Example:

public class Car {
private String make;
private String model;

// Constructor
public Car(String make, String model) {
this.make = make;
this.model = model;
}
}

A method is an ordinary subroutine that is used to perform a specific task. It is declared with a return type and a name, and can take arguments. It is called on an instance of a class and can return a value.

Example:

public class Car {
private String make;
private String model;

// Method
public String getMake() {
return make;
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *