Friday, 15 August 2014

Java: when do we need to use setter methods -



Java: when do we need to use setter methods -

i new java...my question is

to not access info members outside of class utilize access modifier, , why using setter methods modify them outside of 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 utilize public members , instead of modifying them help of setter methods? , in type situations need utilize setters...?

i'm unable understand that..somebody please help me

thanks

it's called encapsulation. mutator methods ensure fields modified in expected , controlled manner.

java

No comments:

Post a Comment