Should I use a constructor to set the default values ​​for class variables that objects will inherit, or does simple assignment have the same effect?

public class Vehicle { private String color; Vehicle() { color = "Red"; } } 

 public class Vehicle { private String color = "Red"; } 

    1 answer 1

    simple assignment has the same effect.

    Yes, it's just syntactic sugar. But if used simultaneously, the constructor will have priority (overwrite the initializer).

    • I see, thanks! - Rumata 5:02 pm