+ 1
What is wrong with this code
6 Answers
+ 8
See comments :
https://code.sololearn.com/cPhSmfQk52iY/?ref=app
+ 8
Not.
In case :
SOP (vehicle.getColor ());
you will have a compiler error because the object/variable "vehicle" is not defined.
Also, value of "color" is not set (private String color;) and if you call getColor() before set of color, getColor() will return default value of color = null
+ 8
"vehicle" is some object or variable. But Vehicle is class and constructors (methods)
Java is case-sensitive language.
+ 5
public class Vehicle {
private String color;
public Vehicle() {
color = "Red";
}
public Vehicle(String color) {
this.color = color;
}
// Setter
public void setColor(String c) {
color = c;
}
// Getter
public String getColor() {
return color;
}
}
If you remove the opening and closing to your multi-line comment and then also remove line 7 from the Vehicle class along with the opening curly brace before it on line 6, your code will work as is. For the most part, you cannot execute code like line 7 outside of a method unless it is being used as an R value to assign to a field or class variable.
+ 2
but @LukArToDo what if I just want to get the result of the vehicle method through
SOP(vehicle.get color());
will it work without using any other method i.e will it print the color of the vehicle directly
0
so, is vehicle a method or a class