1

Consider the following Java code:

public class SomeClass{  
    private int data;  
    public void setData(int data){  
        this.data = data;  
    }  
    public int getData(){  
        return this.data;  
    }
}

In the above code the value of data can be accessed from anywhere. So why not just make the field data public?

1

4 Answers 4

7

Why Getters and Setters?

Many people wonder why we need accessor and mutator methods in Java (a.k.a. getters and setters), why can’t we just access the data directly? But the purpose of encapsulation here is is not to hide the data itself, but the implementation details on how this data is manipulated. So, once more what we want is a way to provide a public interface through which we can gain access to this data. We can later change the internal representation of the data without compromising the public interface of the class. On the contrary, by exposing the data itself, we compromise encapsulation, and therefore, the capacity of changing the ways to manipulate this data in the future without affecting its users. We would create a dependency with the data itself, and not with the public interface of the class. We would be creating a perfect cocktail for trouble when “change” finally finds us.

There are several compelling reasons why we might want to encapsulate access to our fields. The best compendium of these reasons I have ever found is described in Joshua Bloch’s book Effective Java. There in Item 14: Minimize the accessibility of classes and members, he mentions several reasons, which I mention here:

  • You can limit the values that can be stored in a field (i.e. gender must be F or M).
  • You can take actions when the field is modified (trigger event, validate, etc).
  • You can provide thread safety by synchronizing the method.
  • You can switch to a new data representation (i.e. calculated fields, different data type)
  • You could make a field read-only
  • Etc.

However, it is very important to understand that encapsulation is more than hiding fields. In Java we can hide entire classes, by this, hiding the implementation details of an entire API.

My understanding of this important concept was broaden and enriched by my reading of a great article by Alan Snyder called Encapsulation and Inheritance in Object-Oriented Programming Languages which I recommend to all readers.

3

Because you can control how other classes set the data.

You can preform checks on the inputs, or even modify the input according to certain rules you could have.

For example:

public class SomeClass{  
private int data;  
public void setData(int data){  
    if (data < 1000){
        this.data = data;  
    }
    else{
        data = -1;
    }
}  
public int getData(){  
    return this.data;  
}

}

1
  • 1
    Think about just making every variable you ever write (not just primitives) public. You'll inevitably run into a road block where you can't just set var A to var B without some kind of preprocessing going on.
    – The Kraken
    Aug 19, 2013 at 16:06
1

Even if it looks like there's no point in using the getter/setter methods now, since they don't do anything interesting, you need to think about what might happen in the future when the requirements change. You might find that you need to add some validation when setting the value; you might find that you need to change the whole implementation so that the "get" method is actually going to compute a value from several pieces of data, or read it from a file, or something. And when this happens, if you have other classes that are accessing data directly, you will be screwed. You won't be able to change the implementation the way you need to, without a whole lot of pain. I speak from long, bitter experience going through the pain of maintaining other people's sloppy code.

0

Sometimes the situation demands that some variables can access for read or write only purpose. You can achieve it through getter and setter method. If you make the variable public then it can be access and can be changed its value using class object. This type of pattern is call POJO. POJO is an acronym for Plain Old Java Object. The name is used to emphasize that a given object is an ordinary Java Object, not a special object.

This Link can be answered your question and google up the advantages of POJO class for more info.

Not the answer you're looking for? Browse other questions tagged or ask your own question.