Sub topic 7
Controlling access to object state
An object’s state is the information stored in its fields. For example, a lamp object might store whether the lamp is on.
Often, other code needs to use an object without changing its fields directly. Encapsulation is the practice of keeping an object’s state under the control of its class. The class provides methods for the actions it wants to allow.
Private fields and public methods
An access modifier is a word that controls where a class member can be used. A member is a field or a method.
privatekeeps a member available inside its class, but not directly to unrelated classes.publicmakes a member available to other code that can access its class.
A common pattern is to make fields private and provide public methods to use them. This lets the class control how its state changes.
Try a lamp
Create a file named Lamp.java:
public class Lamp {
private boolean on = false;
public void turnOn() {
on = true;
}
public void turnOff() {
on = false;
}
public boolean isOn() {
return on;
}
}A boolean is a type with two values: true and false. The on field stores the lamp’s state.
The turnOn and turnOff methods change that state. The isOn method returns it. A getter is a method that returns a value from an object; isOn is a getter.
The Lamp class declares no constructor, so Java supplies one that lets us create a lamp with new Lamp().
Now create a file named LampDemo.java in the same folder:
public class LampDemo {
public static void main(String[] args) {
Lamp lamp = new Lamp();
System.out.println(lamp.isOn());
lamp.turnOn();
System.out.println(lamp.isOn());
lamp.turnOff();
System.out.println(lamp.isOn());
// lamp.on = true; // This would not compile: on is private.
}
}Run these commands from that folder. javac compiles the Java files, and java runs the program.
javac Lamp.java LampDemo.java
java LampDemoThe output is:
false
true
falseLampDemo can call the public methods, but it cannot access on directly. If you remove the comment marks from lamp.on = true;, the program will not compile.
The public methods give the class one place to manage changes. For example, you could later add a message inside turnOn without changing the code that calls it.
Other access levels
A package is a group of related Java classes. A subclass is a class that extends another class.
| Modifier on a member | Where the member can be used |
|---|---|
| `private` | Inside its class |
| No modifier | Inside the same package |
| `protected` | Inside the same package; subclasses in other packages also have access, with extra rules |
| `public` | From other code that can access its class |
When a member has no access modifier, its access is called package-private. This is different from private: other classes in the same package can use a package-private member.
A public method does not always make every part of an object safe from change. In this example, isOn returns only a boolean value. If a getter returns an object that can be changed, the caller may be able to change that returned object.
Exercise
Exercise
Can LampDemo set lamp.on directly? How can it turn the lamp on instead?
Show answer
No. The on field is private. LampDemo can call lamp.turnOn(), which is public.