why to use get and set method??what's their purpose??

+1 Toka Mohamed · December 12, 2015
why to use get and set method??what's their purpose??

Post a Reply


Oldest  Newest  Rating
+4 See Plus Plus · December 13, 2015
The use of get and set methods is called encapsulation, and it is useful for development teams.
Imagine you have a class called enemy or whatever, basically it might have a public variable called health or something that is equal to 100.  Now, imagine that someone in your development team doesn't know that they shouldn't leave health alone and they edit it so now health is 1000000 or something.
Your whole program could be ruined :(
But, encapsulation is when your PUBLIC(get & set) methods have access to your PRIVATE members(such as health)
If health has get/set methods, the people on your development team will notice and realize that they can edit it without messing up your program, BUT, if health is a private variable without any get/set methods, they will know that they should leave it alone because it has no get/set methods.

sure, you COULD have health as a public variable and NOT have to make get/set methods, but the proper way is to have them.

hope this helps
+2 Kuroodo Ditory · December 14, 2015
Yeah pretty much what See Plus Plus said. It makes code easier to read and use for everyone including yourself, along with protecting the integrity and functionality of the program.

Just imagine a ticket booth at a train station, movie theater, etc.
You don't walk into the booth, print out the tickets and do the transaction all yourself.
You SET (tell the person in the booth) how many tickets you want, then after the transaction, you GET (person in booth gives you) the tickets from the booth
+1 Kuroodo Ditory · December 14, 2015
Some code examples:

This is from a game I made. This is some of the code inside of the player class.


// Getter
public int getPlayerID() {
return playerID;

// Getter
public boolean isDead() {
return isDead;

// Setter
public void setIsJumping(boolean isJumping) {
this.isJumping = isJumping;

// Getter
public boolean isJumping() {
return isJumping;

Note: There are different ways to name the get methods (using the last method isJumping() as an example), the most common way would be getIsJumping(), and the other would be isJumping() as shown above.

It's usually best to name it getIsJumping() but since the name isJumping is almost like a sentence, it makes the code easier to read. Refer to the code below to see what I mean.

if( player.isJumping() ){


if( player.getisJumping() ){


Here is how you would use the set methods and the get methods together.

// This method is called when the player presses the jump key

public void jump() {

if (player.isJumping() == false){




So when we press the jump key, the jump method is called. In there we check if the player is already jumping. If they are not jumping, then now we set isJumping to true (then you have the rest of the code that makes the player jump).
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator