0

I can generate getters and setters in java program and I know that it is used to access private variables. Besides this How will I be able to decide this, at this point I need to create java class with getters and setters.

Nilesh
  • 29
  • 1
  • 4
  • Your question is not clear – Abdul Waheed Apr 12 '17 at 10:00
  • I just wanted to know that how will I be able to realize that" Ok, now I have to make a java class with getters and setters." I'm just confused in this. – Nilesh Apr 12 '17 at 10:02
  • @Nilesh when you want to represent an Entity you have to make a Java class with private fields and public setter/getter methods to access outside that class. – Mehmood Memon Apr 12 '17 at 10:15
  • Possible duplicate of [Why use getters and setters?](http://stackoverflow.com/questions/1568091/why-use-getters-and-setters) – Haem Apr 12 '17 at 10:24

2 Answers2

1

Getter and Setter methods are used for encapsulate the data. it means wrapping the data into single unit.

For example create a ListView with 3 TextView who has different-different value.

so now the question is how will you send the data to your custom ListView adapter. In this case you have to use a beam(has getter-setter methods) class.

EDIT:

Here is another example it will show how to send multiple data by single object from one Activity to another Activity

https://stackoverflow.com/a/7827593/6676466

Community
  • 1
  • 1
Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40
0

in modern java programming you must importantly follow information

hiding this is making the class fields to become private or

protected(to be used by child class) that is why we have getter and setter

method, also getter and setter is useful when you want to have a range check in each data field

if you want to directly access the variable you can create a child class of that parent class and set each fields to become protected

0xDEADBEEF
  • 590
  • 1
  • 5
  • 16