Friday 12 February 2016

why we use getters and setter method


We use getters and setters:

  • for reusability
  • to perform validation in later stages of programming
Getter and setter methods are public interfaces to access private class members

Getter Methods: We can get access to private variables.
Setter Methods: We can modify private fields.
Example:-
Suppose we need to store the details of this Person. This Person has the fields name, age and sex. Doing this involves creating methods for name, age and sex. Now if we need create another person, it becomes necessary to create the methods for name, age, sex all over again. Instead of doing this, we can create a bean class(Person) with getter and setter methods. So tomorrow we can just create objects of this Bean class(Person class) whenever we need to add a new person (see the figure). Thus we are reusing the fields and methods of bean class, which is much better.


Why we use pojo class?
ans- becoz we need some object.

No comments:

Post a Comment