public class Vehicle { int maxSpeed; int wheels; String color; double fuelCapacity; void horn() { System.out.println("Beep!"); } } class MyClass { public static void main(String[ ] args) { Vehicle v1 = new Vehicle(); Vehicle v2 = new Vehicle(); v1.color = "red"; v2.horn(); System.out.println(v1); } } Explain its outcome. I am unable to get the last line of outcome. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

public class Vehicle { int maxSpeed; int wheels; String color; double fuelCapacity; void horn() { System.out.println("Beep!"); } } class MyClass { public static void main(String[ ] args) { Vehicle v1 = new Vehicle(); Vehicle v2 = new Vehicle(); v1.color = "red"; v2.horn(); System.out.println(v1); } } Explain its outcome. I am unable to get the last line of outcome.

16th Aug 2016, 4:20 PM
Himanshu Mittal
Himanshu Mittal - avatar
2 Answers
+ 3
v1 is not a thing that you can "outcome" it. v1 is just. an object of a HOLE class. I mean, it's not a String or Int ect... maybe try specifying what you want to get from that class. so instead of saying (System.out.println(v1)) write (System.out.println(v1.color))
16th Aug 2016, 7:42 PM
Rayen Ochi
Rayen Ochi - avatar
0
the last line actually only prints the hashcode for that vehicle object, as a toString() method was not defined in the vehicle class
16th Aug 2016, 5:01 PM
Edward