0
Construtors | Classes and Object
public class Vehicle { private String color; Vehicle() { this.setColor("Red"); } Vehicle(String c) { this.setColor(c); } // Setter public void setColor(String c) { this.color = c; } // Getter public String getColor() { return color; } } public class Program { public static void main(String[] args) { //color will be "Red" Vehicle v1 = new Vehicle(); //color will be "Green" Vehicle v2 = new Vehicle("Green"); System.out.println(v2.getColor()); } }In the above programme even though we haven't declared 'this' as object the program runs.
3 Réponses
+ 4
@ypk th Great
You better put it in code playground and post the link.
Gives more chances for good answeres.
this is self-reference. No need to declare.
0
So what's the problem?
0
why does that happen was the question. @BlazingMagpie