Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Multiple reasons:</p> <ul> <li><p>If you allow field access like</p> <p>shape.x = 90</p></li> </ul> <p>then you cannot add any logic in future to validate the data.</p> <p>say if x cannot be less than 100 you cannot do it, however if you had setters like</p> <pre><code>public void setShapeValue(int shapeValue){ if(shapeValue &lt; 100){ //do something here like throw exception. } } </code></pre> <ul> <li>You cannot add something like copy on write logic (see <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/CopyOnWriteArrayList.html" rel="noreferrer">CopyOnWriteArrayList</a>)</li> <li>Another reason is for accessing fields outside your class you will have to mark them public, protected or default, and thus you loose control. When data is very much internal to the class breaking <a href="http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29" rel="noreferrer">Encapsulation</a> and in general <a href="http://en.wikipedia.org/wiki/Object-oriented_programming" rel="noreferrer">OOPS</a> methodology.</li> </ul> <p>Though for constants like</p> <pre><code>public final String SOMETHING = "SOMETHING"; </code></pre> <p>you will allow field access as they cannot be changed, for instance variable you will place them with getters, setters.</p> <ul> <li>Another scenario is when you want your Class to be immutable, if you allow field access then you are breaking the immutability of your class since values can be changed. But if you carefully design your class with getters and no setters you keep the immutability intact. </li> </ul> <p>Though in such cases you have to be careful in getter method to ensure you don't give out reference of objects(in case your class have object as instances).</p> <p>We can use the private variables in any package using getters and setters.</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. 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