The example code I have given in this chapter have used the technique of encapsulation. The following code gives a simple example of this

public class Computer{
	private String sProcessor;
	public String getProcessor(){
		return sProcessor;
	public void setProcessor(String sProcessor){

Note how the sProcessor String field is marked as private and can only be accessed externally through the getProcessor and setProcessor methods (known as accessors and mutator methods) or get and set methods. It would be possible to create a class with very similar utility simply by marking the sProcessor String as public and allowing it to be accessed and modified directly from outside the class. The problem with that approach is that it would be easy to accidentally modify that field, and if you needed to add any “sanity checking” code you would have to do it on an “ad-hoc” basis wherever you accessed the fields.

Encapsulation involves separating the interface of a class from its implementation. This means you can't "accidentally" corrupt the value of a field, you have to use a method to change a value

Encapsulation involves hiding data of a class and allowing access only through a public interface

Do not be mislead into thinking that the access control with the private keyword is to do with security. It is not designed to prevent a programmer getting at variables, it is to help avoid unwanted modification.

The separation of interface and implementation makes it easier to modify the code within a class without breaking any other code that uses it.

For the class designer this leads to the ability to modify a class, knowing that it will not break programs that use it. A class designer can insert additional checking routines for "sanity checks" for the modification of fields. I have worked on insurance projects where it was possible for clients to have an age of less than zero. If such a value is stored in a simple field such as an integer, there is no obvious place to store checking routines. If the age were only accessible via set and get methods it will be possible to insert checks against zero or negative ages in such a way that it will not break existing code. Of course as development continues more situations may be discovered that need checking against.

For the end user of the class it means they do not have to understand how the internals work and are presented with a clearly defined interface for dealing with data. The end user can be confident that updates to the class code will not break their existing code. When you are first learning to write Java code it may seem like unnecessary extra work to create accessor and mutator methods, but it is a good habit to get into as you move from a student of the language to a professional practitioner.

Encapsulation is useful for many different programming problems. For example the JDBC drivers used to access different database engines use the same interface to access databases with very different implementations. If a user decides to switch to a different database, e.g from access to Oracle, the interface (the calls to the JDBC driver) stays the same but the way the database behind that driver works may be entirely different.

Last modified: Thursday, 24 July 2014, 2:54 PM