previous |
start |
next
Accessing Instance Fields
- The sayHello method of the Greeter class can
access the private instance field:
public String sayHello()
{
String message = "Hello, " + name + "!";
return message;
}
- Other methods cannot:
public class GreeterTest
{
public static void main(String[] args)
{
. . .
System.out.println(daveGreeter.name); //
ERROR
}
}
- Encapsulation = Hiding data and providing access through
methods
previous |
start |
next