Java is an object-oriented programming language where classes and objects play a central role. Class attributes, also known as fields, are variables declared within a class that describe the properties or characteristics of that class. For example, a Student class may contain attributes like name, age, id, gpa, etc.
In this comprehensive guide, we will cover the following aspects of class attributes in Java:
- What are Class Attributes?
- Declaring Class Attributes
- Accessing Class Attributes
- Modifying Class Attribute Values
- Using the final Keyword with Attributes
- Static Attributes
- Key Differences between Class Attributes and Local Variables
What are Class Attributes?
Class attributes are variables declared directly inside a class, outside any method, constructor or block. They describe the state of an object by storing data associated with that object.
For example:
public class Student {
// Attributes
String name;
int age;
double gpa;
// methods, constructors
}
Here name, age and gpa are attributes of the Student class. Each Student object will have its own distinct copies of these attributes.
Attributes are also called fields or member variables. The terms can be used interchangeably.
Declaring Class Attributes
The syntax for declaring an attribute is:
dataType attributeName;
For example:
int age;
String name;
The data type specifies what type of data can be stored and the attribute name is an identifier used to access the attribute.
Attributes can optionally be initialized with values:
int age = 18;
String name = "Mary";
Multiple attributes can be declared in one line by separating them with commas:
int age, rollNumber;
String name, address;
By convention, attributes should be declared at the top, before methods and constructors.
Accessing Class Attributes
To access attributes of a class, an object of the class must first be created using the new keyword:
Student john = new Student();
Then we access attributes using dot notation:
objectName.attributeName
So to print John‘s age:
System.out.println(john.age);
This prints the value stored in the age attribute of the john object.
Similarly, we can access other attributes of john:
john.name = "John";
john.gpa = 3.5;
This stores values "John" and 3.5 in the name and gpa attributes respectively.
Modifying Attribute Values
The attribute values of an object can be modified by assigning new values, just like normal variables:
john.age = 20; // Modify age
john.name = "John Watson"; // Change name
Each object has its own distinct copy of attributes, so modifying them only affects the current object. Other objects of the same class remain unchanged.
Using the final Keyword
The final keyword makes an attribute constant i.e. its value cannot be changed once assigned.
For example:
final double PI = 3.14;
Trying to modify PI later will cause a compiler error.
final is useful for attributes that should not vary between objects. For example, a Bank class could have final interestRate.
Static Attributes
Static attributes (or class attributes) belong to the class itself instead of individual objects. They have the static keyword:
static int count = 0; // Declare static attribute
Static attributes are shared by all objects. If changes are made from any object, all other objects can see the change.
For example, count can be used to keep track total number of Student objects created.
Differences from Local Variables
There are some key differences between attributes and local variables inside methods:
- Attributes are declared at the class level while local variables are inside methods
- Attributes can have access specifiers like
public,privateetc. but local variables cannot. - Attributes keep their values between method calls while locals variables are temporary
In summary, attributes describe the class while locals describe temporary data used in computations.
Conclusion
- Class attributes store the data representing the state of individual objects
- They are declared at the class level with a datatype and name
- Use dot notation and object references to access attributes
- Attributes can be read/written to query/update state
- final makes attributes immutable
- static attributes belong to the whole class
Understanding attributes is essential for designing and working with classes in Java. This guide covers all the important aspects related to declaring, accessing and modifying attributes.


