Simply said, a class method is a method inside a class.
We can define methods to manipulate data within a class.
Creating a Class Method
Taking from our previous work in Java Class Attributes, we can create a class method that takes care of the level-up process.
For example, if the player levels up, we can increment the amount of health points and magic points the player has, aside from increasing their level.
The process for creating a class method is the same.
class Player {
String name = "Player";
int level = 1;
int exp = 0;
int hp = 20;
int mp = 5;
int monstersKilled = 0;
int timeElapsed = 0;
void levelUp() {
System.out.println("You have leveled up!");
System.out.println("\t" + level + " -> " + (level + 1));
level++;
hp += 10;
mp += 5;
}
}We can also create a class method that accepts inputs or arguments.
For instance, if a player successfully kills a monster, then he accumulates up experience points. We then specify how many monsters he kills.
To make our example easier, we make each monster give the same amount of experience, and that for every 100 experience points, the player levels up.
The method now looks like this:
void killMonster (int monstKilled) {
for (int i = 1; i <= monstKilled; i++) {
exp += 8;
monstersKilled++
System.out.print("\nMonster killed! +8 EXP");
if (exp > level * 100) levelUp();
}
}We can now add it to our class.
In addition to this, I also made a method that returns the player’s current stats, taken in the previous section Accessing Attributes from an Object.
class Player {
String name = "Player";
int level = 1;
int exp = 0;
int hp = 20;
int mp = 5;
int monstersKilled = 0;
int timeElapsed = 0;
void levelUp() {
System.out.println("\n\nYou have leveled up!");
System.out.println("\t" + level + " -> " + (level + 1));
level++;
hp += 10;
mp += 5;
}
void killMonster (int monstKilled) {
for (int i = 1; i <= monstKilled; i++) {
exp += 8;
monstersKilled++
System.out.print("\nMonster killed! +8 EXP");
if (exp > level * 100) levelUp();
}
}
void getPlyrStats () {
System.out.println("\n\nPlayer Info");
System.out.println("Player Name: " + name);
System.out.println("Level " + level);
System.out.println("EXP: " + exp);
System.out.println("HP: " + hp);
System.out.println("MP: " + mp);
System.out.println("\nOther Stats");
System.out.println("Monsters Killed: " + monstersKilled);
System.out.println("Time Alive: " + timeElapsed);
}
}Accessing a Class Method from an Object
Suppose that we have created a new object of the Player class, p1.
public class Main () {
public static void main (String[] args) {
Player p1 = new Player();
}
}Suppose that we want to call the killMonster() method from the object p1.
Just like in accessing class attributes, we would also follow the same syntax:
objName.methodName();Therefore, to indicate that p1 kills 12 monsters, we call the method killMonster() from the object p1 then pass the value 12.
public static void main(String[] args) {
Player p1 = new Player();
p1.killMonsters(12);
}If we add another object of the class Player: p2, it will also have the same methods as p1.
However, they will each have a separate set of attributes.
public static void main(String[] args) {
Player p1 = new Player();
Player p2 = new Player();
p1.killMonsters(12);
p1.getPlyrStats();
p2.getPlyrStats();
}Can you guess what will the above code output?