Java: when do we need to use setter methods
<p>I am new to Java...My question is</p>

<p>To do not access the data members outside of the class we use Access modifier, And why we are using setter methods to modify them outside of the class.
Ex:-</p>

<pre><code>public class Test
{

       private int a=10;
       private int b=20;
       public void sum(int x, int y)
       {
           System.out.println("Sum is:"+(x+y));
       }
       public void setA(int a)
       {
           this.a=a;
       }
       public void setB(int b)
       {
           this.b=b;
       } 
}
</code></pre>

<p>Like these type of cases why don't we use public members , instead of modifying them with the help of setter methods? And in which type situations we need to use setters...?</p>

<p>I'm unable to understand that..Somebody please help me</p>

<p>Thanks</p>
