Java

Class Attributes in Java | Explained

Java follows the object-oriented programming approach that revolves around classes and objects. Java Classes can have some fields and methods that represent the individual properties and behavior/actions of the class. The fields also known as class attributes are nothing but the variables declared within the class. For instance, the Student is a class then the student’s roll no, name, section, etc. can be the class attributes of the Student Class.

This write-up presents a comprehensive overview of Class attributes and for this purpose, it explains the following aspects of Class Attributes:

Let’s start!

What is a Class Attribute

In java, a variable within the class is referred as a class attribute and the class attributes are also known as fields. Let’s understand the concept of a class attribute with the help of an example. Let’s say we have a class named Employee as shown in the below-given snippet:

public class Employee {
   String empName = "John";
   int empId = 5;
   int empAge = 32;
}

Here in the above snippet empName, empId, empAge, are the attributes of the “Employee” class.

How to Access the Class Attributes

The attributes of the class can be accessed with the help of the class’ object. For better understanding consider the below code snippet which elaborates the basic syntax of accessing a class attribute:

empObj.empName;

In the above snippet empObj is an object of the employee class and empName is an attribute of the same class. So, collectively the object empObj is used in accessing the value of class attribute empName.

Example

The below code snippet shows how to access the class attributes:

public static void main(String[] args) {
   Employee empObj = new Employee();
   System.out.println(empObj.empName);
   System.out.println(empObj.empId);
   System.out.println(empObj.empAge);
}

The above snippet firstly creates the object of the Employee class and afterwards it access the class attributes by using the object of the Employee class.

The complete code and its output will be:

The output verifies that the class attributes are successfully accessed by using class objects.

How to Modify/override the Class Attributes

We can modify or override the class attributes with new values.

Example

In this example we will modify the values of empName, and empAge:

public class Employee {

   String empName = "John";
   int empId = 5;
   int empAge = 32;

   public static void main(String[] args) {
      Employee empObj = new Employee();
      empObj.empAge = 30;
      empObj.empName = "Joe";
      System.out.println("Employee Name: " + empObj.empName);
      System.out.println("Employee ID: " + empObj.empId);
      System.out.println("Employee Age: " + empObj.empAge);
   }
}

In the above snippet the initial values of empId, and empName are 32 and Joe, however we modified both these values in the main function:

Output verified that the initial values have been overridden with the new values.

How to use final Keyword with Class Attributes

In order to prevent a class attribute from being overridden a final keyword can be used.

Example

Let’s modify the previous example a little bit and add the final keyword with empName class attribute:

final String empName = "John";

Now, consider the below snippet to understand what will happen if we try to modify the value of attribute declared with final keyword:

The output verifies that an error occurs when we try to access and change the empName attribute.

How to Modify the Specific Value

If we have multiple objects of the class then modifying the attribute value of one object wouldn’t affect the values of others.

Example

In the below snippet, we create two objects of the same class and modifying the value of one attribute in one object wouldn’t modify the value of that attribute for other objects.

public class Employee {

   String empName = "John";
   int empId = 5;
   int empAge = 32;

   public static void main(String[] args) {
      Employee empObj = new Employee();
      Employee empObj1 = new Employee();
      empObj.empName = "Joe";
      System.out.println("Employee Name: " + empObj.empName);
      System.out.println("Employee Name: " + empObj1.empName);
   }
}

The below-given snippet provides the complete code along with output:

From the output, it is clear that empObj1 gets the unchanged(initial) value which authenticates that the modifying the value in one object doesn’t affect the others.

Conclusion

The variables created within the Java classes are referred as class attributes or fields. Class attributes can be accessed with the help of object of the class and utilizing the dot (.) syntax. Values of the class attributes can be modified by specifying a new value to the attributes, however, the final keyword restricts us to modify the value of class attributes. This write-up presents a detailed overview of class attributes with the help of some examples. For a profound understanding of the concepts, descriptive screenshots are also provided with examples.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.