0
java constructors
Can anyone plz explain what is the differece betweeen the following line of codes Vehicle(String c){ this.setColor(c); } and public void setColor(String c){ this.color=c; }
3 Respostas
+ 1
The first function is called the constructor of class Vehicle...and the second function is the method of that class.....
Constructors are called when an object of the class are created....
Vehicle obj = new Vehicle('blue');
The methods can be called through the objects.
U can call setColor method by
obj.setColor('red');
The reason object is not used here is because you can access all the methods of the class from within the class without object (Except the static methods)
+ 1
the first code is constractor which sets the color when new object is created.
The second code is a method which changes the color to another color.
0
nznxmx