-
Hajipur, Bihar, 844101
In Java, attributes are variables that belong to a class. They are also called fields or data members. Attributes hold the state or characteristics of an object, and each object can have its own unique values for these attributes.
If you think of a class as a blueprint and an object as the final product, then attributes are the properties that describe each product. For example, a Car class can have attributes like brand, color, and speed. Each car object created from this class will have its own brand, color, and speed values.
This tutorial will help you understand how class attributes work, how to access and modify them, and the difference between instance and static attributes.
A class attribute is a variable that belongs to either the class itself or to each object of that class. These variables store information that defines the object’s state.
For example:
class Student {
String name; // attribute
int age; // attribute
}
Here, name and age are class attributes. Every object created from the Student class will have its own copy of these attributes.
You can access class attributes through objects using the dot operator (.).
Example:
class Student {
String name;
int age;
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Riya";
s1.age = 20;
System.out.println(s1.name);
System.out.println(s1.age);
}
}
Output:
Riya
20
Here, s1.name and s1.age access the attributes of the Student object s1.
After creating an object, you can change its attribute values easily using the dot operator.
class Car {
String brand = "Honda";
int speed = 100;
public static void main(String[] args) {
Car c1 = new Car();
System.out.println("Before change: " + c1.brand);
c1.brand = "BMW"; // changing the attribute value
System.out.println("After change: " + c1.brand);
}
}
Output:
Before change: Honda
After change: BMW
This shows that attribute values are not fixed; they can be updated anytime after object creation.
There are two types of attributes in Java:
Instance attributes belong to a specific object. Each object has its own separate copy of these attributes.
For example:
class Dog {
String name;
int age;
}
If you create two Dog objects, each can have a different name and age.
Dog d1 = new Dog();
d1.name = "Luna";
d1.age = 3;
Dog d2 = new Dog();
d2.name = "Bella";
d2.age = 2;
Each dog has different attribute values, even though they come from the same class.
A static attribute belongs to the class itself, not to individual objects. All objects of that class share the same value for the static variable.
To declare a static attribute, use the keyword static.
Example:
class Student {
String name;
static String college = "ABC University"; // static attribute
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Anjali";
Student s2 = new Student();
s2.name = "Tina";
System.out.println(s1.name + " studies at " + Student.college);
System.out.println(s2.name + " studies at " + Student.college);
}
}
Output:
Anjali studies at ABC University
Tina studies at ABC University
Since college is static, both s1 and s2 share the same value. You can access static attributes using either the class name (Student.college) or through an object, but the class name approach is preferred.
If you don’t assign a value to an attribute, Java assigns a default value depending on its data type.
| Data Type | Default Value |
|---|---|
| int | 0 |
| float | 0.0 |
| boolean | false |
| char | '\u0000' (null character) |
| Object (String, arrays, etc.) | null |
Example:
class Example {
int number;
boolean status;
String text;
void show() {
System.out.println(number);
System.out.println(status);
System.out.println(text);
}
public static void main(String[] args) {
Example e = new Example();
e.show();
}
}
Output:
0
false
null
It’s not always good practice to make attributes directly accessible. Instead, you can use methods to set or get values. This helps maintain encapsulation and improves code safety.
Example:
class Employee {
private int salary; // private attribute
// Setter method
public void setSalary(int s) {
salary = s;
}
// Getter method
public int getSalary() {
return salary;
}
}
public class Main {
public static void main(String[] args) {
Employee e1 = new Employee();
e1.setSalary(50000);
System.out.println("Salary: " + e1.getSalary());
}
}
This way, you control how the attribute is accessed and modified, protecting your data from unintended changes.
Let’s create a complete example to see how attributes work inside a class.
class Mobile {
String brand;
int price;
static String type = "Smartphone";
void showDetails() {
System.out.println("Brand: " + brand);
System.out.println("Price: " + price);
System.out.println("Type: " + type);
}
public static void main(String[] args) {
Mobile m1 = new Mobile();
m1.brand = "Samsung";
m1.price = 25000;
Mobile m2 = new Mobile();
m2.brand = "Apple";
m2.price = 90000;
m1.showDetails();
System.out.println();
m2.showDetails();
}
}
Output:
Brand: Samsung
Price: 25000
Type: Smartphone
Brand: Apple
Price: 90000
Type: Smartphone
Both objects have their own brand and price, but they share the same static type.
Attributes represent the state of an object.
Each object has its own copy of instance attributes.
Static attributes are shared across all objects.
Access attributes using the dot operator (.).
For secure code, prefer using private attributes with getter/setter methods.
Java assigns default values to attributes if not initialized.
In this tutorial, you learned everything about Java class attributes — how to declare, access, and modify them. You also understood the difference between instance and static attributes, and how encapsulation can protect them.
Attributes are at the heart of every class, as they hold the data that defines an object’s identity. Understanding them well is key to mastering Java OOP and will help you when working with constructors, methods, and access modifiers in upcoming chapters.
Create a class called Car with attributes brand, color, and price. Assign values to them and display the details using a method showDetails().
Write a Java program to create a Student class with attributes name, age, and marks. Assign values through objects and print the student’s information.
Create a class named Employee with attributes id, name, and salary. Demonstrate how you can change the value of salary after object creation.
Develop a class Product with attributes productName, price, and category. Add a static attribute storeName and display it with each product’s details.
Write a program to create a Book class that has attributes title, author, and price. Use methods to set and get each attribute’s value.
Create a class called Mobile with a static attribute brand and instance attributes model and price. Show how static and instance attributes behave differently with multiple objects.
Build a class Laptop with attributes ram, processor, and price. Add a method updatePrice() that changes the price and prints the updated value.
Write a Java program to create a BankAccount class with attributes accountNumber, balance, and a static bankName. Demonstrate how all accounts share the same bank name but different balances.
Design a class House with attributes ownerName, area, and color. Use the dot operator to modify the color of the house object after creation and print the new details.
Create a class Student with private attributes rollNumber and marks. Use getter and setter methods to assign and display their values.