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.

9th Jan 2018, 9:38 AM
YAKSH PATEL (aka YKP The GREAT)
YAKSH PATEL (aka YKP The GREAT) - avatar
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.
9th Jan 2018, 10:14 AM
Oma Falk
Oma Falk - avatar
0
So what's the problem?
9th Jan 2018, 10:16 AM
BlazingMagpie
BlazingMagpie - avatar
0
why does that happen was the question. @BlazingMagpie
9th Jan 2018, 10:42 AM
YAKSH PATEL (aka YKP The GREAT)
YAKSH PATEL (aka YKP The GREAT) - avatar