Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

use of getter n setter methods

 
veena bijur
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


hi, could any one please say what is the use of getter() and setter()
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, could any one please say what is the use of getter() and setter()


You first. What do you think they're for? They're only one line of code each.
 
veena bijur
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.data memeber name and salary are declared as private
2. those can be accessed only within the class.
3.. to make it accessible to other class we go for setter() and getter()



 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
veena bijur wrote:1.data memeber name and salary are declared as private
2. those can be accessed only within the class.
3.. to make it accessible to other class we go for setter() and getter()


Correct.

Note, however, that in general it's not a good idea to just throw getters and setters on every member as a matter of course. In most objects other than DTOs and JavaBeans, you won't directly expose the member variables like that. Rather, you'll provide methods that implement behavior, and manipulate or expose some aspect or form of the members as part of that behavior.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic