20th Mar 2018, 6:14 AM
Devansh Gupta
Devansh Gupta - avatar
6 Answers
20th Mar 2018, 7:05 AM
LukArToDo
LukArToDo - avatar
+ 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
20th Mar 2018, 8:04 AM
LukArToDo
LukArToDo - avatar
+ 8
"vehicle" is some object or variable. But Vehicle is class and constructors (methods) Java is case-sensitive language.
20th Mar 2018, 8:08 AM
LukArToDo
LukArToDo - avatar
+ 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.
20th Mar 2018, 6:53 AM
ChaoticDawg
ChaoticDawg - avatar
+ 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
20th Mar 2018, 7:54 AM
Devansh Gupta
Devansh Gupta - avatar
0
so, is vehicle a method or a class
20th Mar 2018, 8:06 AM
Devansh Gupta
Devansh Gupta - avatar