0
What is the v1. color="red" for. can we output it without creatiNG Methods like we did for horn().
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(); } }
1 Odpowiedź
+ 1
Hello Wedo Ru
Your vehicle has a color. Because it is not private you can assign a value to color.
Just try this:
System.out.println(v1.color);
v1.color = "red";
System.out.println(v1.color);
Maybe not the best example but it shows you what you can do with fields (variables in a class).
If color would be private you would need methods to manipulate/return the value (see lesson about getter/setter).
Maybe this article is interesting for you:
https://jenkov.com/tutorials/java/fields.html