aspose file tools*
The moose likes Java in General and the fly likes use of getter n setter methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "use of getter n setter methods" Watch "use of getter n setter methods" New topic
Author

use of getter n setter methods

veena bijur
Ranch Hand

Joined: May 16, 2011
Posts: 67


hi, could any one please say what is the use of getter() and setter()
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: May 16, 2011
Posts: 67
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: use of getter n setter methods