Now let's try to directly access the fields from another class and see what happens:
class CarTest8 {
public static void main(String args[]) {
Car c = new Car("New York A45 636", 100.0);
c.licensePlate = "New York A45 636";
c.speed = 0.0;
c.maxSpeed = 123.45;
System.out.println(c.licensePlate + " is moving at " + c.speed +
" kilometers per hour.");
c.floorIt();
System.out.println(c.licensePlate + " is moving at " + c.speed +
" kilometers per hour.");
}
}
Here's what happens when you try to compile it against the revised Car
class:
% javac Car.java
% javac CarTest8.java
CarTest8.java:7: Variable licensePlate in class Car not accessible from class CarTest8.
c.licensePlate = "New York A45 636";
^
CarTest8.java:8: Variable speed in class Car not accessible from class CarTest8.
c.speed = 0.0;
^
CarTest8.java:9: Variable maxSpeed in class Car not accessible from class CarTest8.
c.maxSpeed = 123.45;
^
CarTest8.java:11: Variable licensePlate in class Car not accessible from class CarTest8.
System.out.println(c.licensePlate + " is moving at " + c.speed +
^
CarTest8.java:11: Variable speed in class Car not accessible from class CarTest8.
System.out.println(c.licensePlate + " is moving at " + c.speed +
^
CarTest8.java:16: Variable licensePlate in class Car not accessible from class CarTest8.
System.out.println(c.licensePlate + " is moving at " + c.speed +
^
CarTest8.java:16: Variable speed in class Car not accessible from class CarTest8.
System.out.println(c.licensePlate + " is moving at " + c.speed +
^
7 errors
%