An attribute is a part of a class that contain data.

In short, these are variables declared within a class.

We’ll see how we can use these to our advantage as if we try to create a class containing the attributes of a player from a fictional game we’re trying to develop.

Creating a Class Attribute

Suppose that we have a Player class.

Inside the player class, we can set up data that a player has.

For instance:

  • the player’s current level,
  • experience points,
  • health points,
  • magic points,
  • and other data such as
    • the number of monsters killed,
    • or the time elapsed from playing the game.

To do this, let us first create an empty Player class.

class Player {
	
}

Now inside the Player class, we can now add the data for our player.

The process for declaring class attributes is the same for declaring variables inside a function.

class Player {
	String name = "Player";
	int level = 1;
	int exp = 0;
	int hp = 20;
	int mp = 5;
	int monstersKilled = 0;
	int timeElapsed = 0;
}

It is important that we initialize these variables as well.

Here, we simply placed the default values for each stats.

Accessing Attributes from an Object

One good thing about class attributes is that it carries along when we make a new object of that class.

Suppose we create a new object p1 (shorthand for Player 1) from the class Player.

public class Main () {
	public static void main (String[] args) {
		Player p1 = new Player();
	}
}

To access the attributes of the object p1 (for instance, the level attribute), we can simply use the dot notation.

public static void main (String[] args) {
	Player p1 = new Player();
	
	System.out.println("Level " + p1.level);
}

We can also do the same for the other attributes.

To access a class attribute like a variable, we can simply follow this syntax:

objName.classAttributeName

Noting this, we can display the character information sheet, that is all attributes of the object p1 as follows:

public static void main (String[] args) {
	Player p1 = new Player();
	
	System.out.println("Player Info");
	System.out.println("Player Name: " + p1.name);
	System.out.println("Level " + p1.level);
	System.out.println("EXP: " + p1.exp);
	System.out.println("HP: " + p1.hp);
	System.out.println("MP: " + p1.mp);
	System.out.println("\nOther Stats");
	System.out.println("Monsters Killed: " + p1.monstersKilled);
	System.out.println("Time Alive: " + p1.timeElapsed);
}

Assigning Values to a Class Attribute

As we’ve seen, we successfully displayed the character stats in the previous section.

However, these are the default stats. What if we wanted the player to level up, or even change the amount of experience they currently have?

We can do that by assigning values to a class attribute, and that is no different than assigning/changing values to a variable.

public static void main (String[] args) {
	Player p1 = new Player();
	
	System.out.println("Level: " + p1.level);
	
	p1.level += 1;
	
	System.out.println("Level: " + p1.level);
}

Attributes of Multiple Objects of the Same Class

Suppose that we define a new Player object, p2.

public static void main (String[] args) {
	Player p1 = new Player();
	Player p2 = new Player();
}

We can access the attributes of each object just like before:

public static void main (String[] args) {
	Player p1 = new Player();
	Player p2 = new Player();
	
	System.out.print("Player 1: " + p1.name); // Outputs Player 1: Player
	System.out.print("Player 2: " + p2.name); // Outputs Player 2: Player
}

We can also change values of an object’s attribute.

For example, we can give each player different names:

public static void main (String[] args) {
	Player p1 = new Player();
	Player p2 = new Player();
	
	p1.name = "Warlock";
	p2.name = "Assassin";
	
	System.out.print("Player 1: " + p1.name); // Outputs Player 1: Warlock
	System.out.print("Player 2: " + p2.name); // Outputs Player 2: Assassin
}

This also makes it possible for them to have separate player stats.

Therefore, we can have multiple players each having their own independent statuses.