Master java skills

Class attributes and methods

A class consists of attributes and methods. Let’s learn more about them.

1. Attributes

Class attributes are variables defined in the class. These attributes define the state of the object at a particular time. Attributes can be either primitive type like int, byte, char, long, double etc. or Java api classes like String, Boolean etc, or custom type like Doctor, Student etc. Let’s see few examples of class attributes.

public class SportsPerson {
      
      private int id;
      private String name;

}

The above class has two attributes of type int (id) and String (name). Similarly, a class can have n no of attributes.

Note -> When we don’t assign any value to the class attributes, they take default values according to their data type. For example, int has a default value ‘0’ whereas String has default value as null.

public class SportsPerson {
      
      private int id = 10;
      private String name = "Arjun";
     
      public static void main(String[] args) {
		
    	  SportsPerson sp = new SportsPerson();
    	  System.out.println("Player Id : " + sp.id);
    	  System.out.println("Name : " + sp.name);
    	  
	}
}

In the above example, the playerId and name have been assigned initial values.

Output :

Player Id : 10
Name : Arjun

1.1 Reassigning attribute values

public class SportsPerson {
      
      private int id = 10;
      private String name = "Arjun";

      public static void main(String[] args) {
		
    	  SportsPerson sp = new SportsPerson();
    	  sp.id = 20;
    	  sp.name = "Shankar";
    	  System.out.println("Player Id : " + sp.id);
    	  System.out.println("Name : " + sp.name);
    	  
	}

}

Output :

Player Id : 20
Name : Shankar

1.2 Class attributes for multiple objects of the same class

public class SportsPerson {
      
      private int id = 10;
      private String name = "Arjun";

      public static void main(String[] args) {
		
    	  SportsPerson sp1 = new SportsPerson();
    	  SportsPerson sp2 = new SportsPerson();
    	  sp2.playerId = 20;
    	  sp2.name = "Shankar";
    	  System.out.println("Player Id of sportsPerson1 is : " + sp1.id);
    	  System.out.println("Name of sportsPerson1 is : " + sp1.name);
    	  System.out.println("Player Id of sportsPerson2 is : " + sp2.id);
    	  System.out.println("Name of sportsPerson2 is : " + sp2.name);
    	  
	}

}

Output :

Player Id of sportsPerson1 is : 10
Name of sportsPerson1 is : Arjun
Player Id of sportsPerson2 is : 20
Name of sportsPerson2 is : Shankar

Types of Attributes

We can broadly divide attributes in two groups:

  1. Instance attributes
  2. Static attributes

Instance Attributes

Instance attributes are associated with instances/objects of the class. Every object has its own copy of instance variables. One object’s values will be specific to that object. Let’s take one example

package com.sks;

public class Cricketer {

	private String name;
	private int age;
	private String type;

	public static void main(String[] args) {

		Cricketer cricketer1 = new Cricketer();
		cricketer1.name = "Sachin Tendulkar";
		cricketer1.age = 35;
		cricketer1.type = "Batsman";

		Cricketer cricketer2 = new Cricketer();
		cricketer2.name = "Shane Warne";
		cricketer2.age = 38;
		cricketer2.type = "Bowler";

		System.out.println("Cricketer 1 name : " + cricketer1.name + ", age : " + cricketer1.age + ", type : " + cricketer1.type);
		System.out.println("Cricketer 2 name : " + cricketer2.name + ", age : " + cricketer2.age + ", type : " + cricketer2.type);

	}
}

Output:

Cricketer 1 name : Sachin Tendulkar, age : 35, type : Batsman
Cricketer 2 name : Shane Warne, age : 38, type : Bowler

Static Attributes

Static attributes are associated with the class itself, which means their values will be common for all the objects of that class. This is the reason static attributes are also called Class attributes. Class attributes are accessed using name of the class. In order to declare static attributes, we have to use static keyword after the access modifier.

package com.sks;

public class Cricketer {

	private String name;
	private int age;
	private String type;
	private static String sportName;  //This is static or class variable

	public static void main(String[] args) {

		Cricketer.sportName = "Cricket"; //set only once and it is available to all the objects
		Cricketer cricketer1 = new Cricketer();
		cricketer1.name = "Sachin Tendulkar";
		cricketer1.age = 35;
		cricketer1.type = "Batsman";

		Cricketer cricketer2 = new Cricketer();
		cricketer2.name = "Shane Warne";
		cricketer2.age = 38;
		cricketer2.type = "Bowler";

		System.out.println("Cricketer 1 name : " + cricketer1.name + ", age : " + cricketer1.age + ", type : " + cricketer1.type + ", sportName : " + Cricketer.sportName);
		System.out.println("Cricketer 2 name : " + cricketer2.name + ", age : " + cricketer2.age + ", type : " + cricketer2.type + ", sportName : " + Cricketer.sportName);

	}
}

Output:

Cricketer 1 name : Sachin Tendulkar, age : 35, type : Batsman, sportName : Cricket
Cricketer 2 name : Shane Warne, age : 38, type : Bowler, sportName : Cricket

2. Methods

Methods in java have been explained in detail in the next chapter. Follow the link

Methods In Java – Java Training School