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){

player.setIsJumping(true);

}

}

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).

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

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

1

Java / Android Development

127,870 followers

About

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