A constructor is a special type of method that is used to create an instance of an object. It is invoked when an instance of the object is created and it is used to set up the initial state of the object.

A method is a block of code that performs a specific task and is associated with an object. It is invoked by calling the method on the object.

Example:

Constructor:

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

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

Method:

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

public void setMake(String make) {
this.make = make;
}

public void setModel(String model) {
this.model = model;
}
}

Leave a Reply

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