In the below example we can initialize class variable using the constructor i.e., this keyword or by through an object. Can someone answer then why do we use constructor to pass the value or initialize a variable:
public class Car {
String color;
int price;
public static void main(String[] args) {
Car obj = new Car();
obj.color = "Red";
obj.price = 80;
System.out.println(obj.color + " "+ obj.price);
Car obj1 = new Car();
obj1.color = "White";
obj1.price = 70;
System.out.println(obj1.color+" "+obj1.price);
}
}