Java Class Attributes
Java class attributes are variables declared inside a class but outside any method. They are also called fields, and they store the data that belongs to each object.
Attributes describe what an object has. For example, a student object might have a name, grade level, and score.
Declare Attributes
To create an attribute, write a variable declaration inside the class body. The declaration includes the type and the attribute name.
class Student {
String name;
int grade;
double average;
}
public class Main {
public static void main(String[] args) {
Student student = new Student();
student.name = "Maya";
student.grade = 9;
student.average = 91.5;
System.out.println(student.name);
System.out.println(student.grade);
System.out.println(student.average);
}
}
Output:
Maya
9
91.5
The Student class has three attributes: name, grade, and average. After creating a Student object with new Student(), the program assigns values to those attributes.
Use The Dot Operator
Use the dot operator, ., to access an attribute through an object variable.
student.namemeans thenameattribute of the object stored instudent.student.grade = 9;changes that object’sgradeattribute.System.out.println(student.average);reads that object’saverageattribute.
The object variable comes before the dot. The attribute name comes after the dot.
Each Object Has Its Own Values
A class can create many objects. Each object gets its own copy of the instance attributes, so changing one object does not change another object.
class Player {
String name;
int score;
}
public class Main {
public static void main(String[] args) {
Player first = new Player();
first.name = "Ava";
first.score = 12;
Player second = new Player();
second.name = "Leo";
second.score = 8;
first.score = first.score + 5;
System.out.println(first.name + ": " + first.score);
System.out.println(second.name + ": " + second.score);
}
}
Output:
Ava: 17
Leo: 8
Both objects were created from the same Player class, but their attribute values are separate. Increasing first.score does not affect second.score.
Default Attribute Values
If you create an object and do not assign a value to an attribute, Java gives the attribute a default value. Numeric fields start at 0 or 0.0, boolean fields start as false, and object references such as String start as null.
class Settings {
int volume;
boolean muted;
String theme;
}
public class Main {
public static void main(String[] args) {
Settings settings = new Settings();
System.out.println(settings.volume);
System.out.println(settings.muted);
System.out.println(settings.theme);
}
}
Output:
0
false
null
These defaults are useful to know, but it is usually clearer to assign meaningful values before using an object in real programs.
Attributes And Local Variables
An attribute belongs to an object and is declared in a class. A local variable is declared inside a method and exists only while that method is running.
| Kind | Where It Is Declared | How It Is Used |
|---|---|---|
| Attribute | Inside a class, outside methods | Stores object state |
| Local variable | Inside a method or block | Stores temporary method data |
In beginner code, attributes are often shown without an access modifier. Later lessons will introduce private fields and methods that protect object data.
Common Mistakes
- Declaring an attribute inside
main; that creates a local variable, not an object attribute. - Forgetting to create an object before using its attributes.
- Expecting two different objects to share the same instance attribute value.
- Using a
nullreference value as if it were a complete object.
Takeaway: class attributes are fields that store an object’s state, and each object created from the class has its own attribute values.
