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; }

4th Apr 2018, 3:31 AM
Amitesh Shrey
Amitesh Shrey - avatar
3 Answers
+ 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)
4th Apr 2018, 4:15 AM
Mohammed Numan
+ 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.
4th May 2018, 10:42 PM
Joash N
Joash N - avatar
0
nznxmx
4th Jul 2021, 9:25 AM
Pengson, Emmanuel T.