I am new to Java...My question is
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:-
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;
}
}
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...?
I'm unable to understand that..Somebody please help me
Thanks