Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Why Getters and Setters?</p> <p>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.</p> <p>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 <a href="http://rads.stackoverflow.com/amzn/click/0321356683" rel="nofollow">Effective Java</a>. There in Item 14: Minimize the accessibility of classes and members, he mentions several reasons, which I mention here:</p> <ul> <li>You can limit the values that can be stored in a field (i.e. gender must be F or M).</li> <li>You can take actions when the field is modified (trigger event, validate, etc).</li> <li>You can provide thread safety by synchronizing the method.</li> <li>You can switch to a new data representation (i.e. calculated fields, different data type)</li> <li>You could make a field read-only</li> <li>Etc.</li> </ul> <p>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.</p> <p>My understanding of this important concept was broaden and enriched by my reading of a great article by Alan Snyder called <a href="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.36.8552&amp;rep=rep1&amp;type=pdf" rel="nofollow">Encapsulation and Inheritance in Object-Oriented Programming Languages</a> which I recommend to all readers.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload