codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

26 / 65 topics
24Java OOP25Java Classes/Objects26Java Class Attributes27Java Class Methods28Java Constructors29Java Modifiers30Java Encapsulation31Java Packages / API
Tutorials/Java Programming/Java Class Attributes
☕Java Programming

Java Class Attributes

Updated 2026-05-12
15 min read

Java Class Attributes

In object-oriented programming, attributes are variables that hold data specific to an object. Understanding how to manage these attributes is crucial for creating robust and maintainable Java programs. This tutorial will guide you through accessing, modifying, and handling multiple attributes within a class.

Introduction

Attributes in Java classes represent the state of an object. They can be accessed and modified using methods or directly (with appropriate access modifiers). Proper management of attributes ensures encapsulation, which is a fundamental principle of OOP. This tutorial will cover:

  • Accessing Attributes: How to retrieve attribute values.
  • Modifying Attributes: Techniques for changing attribute values.
  • Multiple Attributes: Managing multiple attributes within a class.

Accessing Attributes

Attributes can be accessed using the dot notation (.) on an object instance. The access depends on the attribute's visibility (public, private, protected).

Example: Accessing Public Attributes

Java
1class Car {
2 public String color;
3 public int year;
4
5 public Car(String color, int year) {
6 this.color = color;
7 this.year = year;
8 };
9}
10
11public class Main {
12 public static void main(String[] args) {
13 Car myCar = new Car("Red", 2020);
14 System.out.println("Color: " + myCar.color);
15 System.out.println("Year: " + myCar.year);
16 }
17}
Output
Color: Red
Year: 2020

Example: Accessing Private Attributes

Private attributes cannot be accessed directly from outside the class. They require public getter methods.

Java
1class Car {
2 private String color;
3 private int year;
4
5 public Car(String color, int year) {
6 this.color = color;
7 this.year = year;
8 }
9
10 public String getColor() {
11 return color;
12 }
13
14 public int getYear() {
15 return year;
16 }
17}
18
19public class Main {
20 public static void main(String[] args) {
21 Car myCar = new Car("Blue", 2018);
22 System.out.println("Color: " + myCar.getColor());
23 System.out.println("Year: " + myCar.getYear());
24 }
25}
Output
Color: Blue
Year: 2018

Modifying Attributes

Attributes can be modified using setter methods or directly (for public attributes).

Example: Modifying Public Attributes

Java
1class Car {
2 public String color;
3 public int year;
4
5 public Car(String color, int year) {
6 this.color = color;
7 this.year = year;
8 }
9}
10
11public class Main {
12 public static void main(String[] args) {
13 Car myCar = new Car("Green", 2019);
14 System.out.println("Original Color: " + myCar.color);
15
16 // Modify the attribute
17 myCar.color = "Yellow";
18 System.out.println("Modified Color: " + myCar.color);
19 }
20}
Output
Original Color: Green
Modified Color: Yellow

Example: Modifying Private Attributes

Private attributes should be modified using setter methods.

Java
1class Car {
2 private String color;
3 private int year;
4
5 public Car(String color, int year) {
6 this.color = color;
7 this.year = year;
8 }
9
10 public void setColor(String color) {
11 this.color = color;
12 }
13
14 public void setYear(int year) {
15 this.year = year;
16 }
17}
18
19public class Main {
20 public static void main(String[] args) {
21 Car myCar = new Car("Black", 2017);
22 System.out.println("Original Color: " + myCar.color);
23
24 // Modify the attribute using setter
25 myCar.setColor("White");
26 System.out.println("Modified Color: " + myCar.color);
27 }
28}
Output
Original Color: Black
Modified Color: White

Multiple Attributes

A class can have multiple attributes, each serving a different purpose. Managing these attributes effectively is crucial for maintaining clean and efficient code.

Example: Handling Multiple Attributes

Java
1class Car {
2 private String make;
3 private String model;
4 private int year;
5
6 public Car(String make, String model, int year) {
7 this.make = make;
8 this.model = model;
9 this.year = year;
10 }
11
12 public String getMake() {
13 return make;
14 }
15
16 public void setMake(String make) {
17 this.make = make;
18 }
19
20 public String getModel() {
21 return model;
22 }
23
24 public void setModel(String model) {
25 this.model = model;
26 }
27
28 public int getYear() {
29 return year;
30 }
31
32 public void setYear(int year) {
33 this.year = year;
34 }
35}
36
37public class Main {
38 public static void main(String[] args) {
39 Car myCar = new Car("Toyota", "Corolla", 2021);
40 System.out.println("Make: " + myCar.getMake());
41 System.out.println("Model: " + myCar.getModel());
42 System.out.println("Year: " + myCar.getYear());
43
44 // Modify attributes
45 myCar.setMake("Honda");
46 myCar.setModel("Civic");
47 myCar.setYear(2022);
48
49 System.out.println("
50Updated Details:");
51 System.out.println("Make: " + myCar.getMake());
52 System.out.println("Model: " + myCar.getModel());
53 System.out.println("Year: " + myCar.getYear());
54 }
55}
Output
Make: Toyota
Model: Corolla
Year: 2021

Updated Details:
Make: Honda
Model: Civic
Year: 2022

Practical Example

Let's create a practical example that demonstrates the use of multiple attributes, accessors, and mutators.

Java
1class Employee {
2 private String name;
3 private int age;
4 private double salary;
5
6 public Employee(String name, int age, double salary) {
7 this.name = name;
8 this.age = age;
9 this.salary = salary;
10 }
11
12 public String getName() {
13 return name;
14 }
15
16 public void setName(String name) {
17 this.name = name;
18 }
19
20 public int getAge() {
21 return age;
22 }
23
24 public void setAge(int age) {
25 if (age > 0) {
26 this.age = age;
27 } else {
28 System.out.println("Invalid age");
29 }
30 }
31
32 public double getSalary() {
33 return salary;
34 }
35
36 public void setSalary(double salary) {
37 if (salary >= 0) {
38 this.salary = salary;
39 } else {
40 System.out.println("Invalid salary");
41 }
42 }
43
44 public void displayInfo() {
45 System.out.println("Name: " + name);
46 System.out.println("Age: " + age);
47 System.out.println("Salary: $" + salary);
48 }
49}
50
51public class Main {
52 public static void main(String[] args) {
53 Employee emp = new Employee("John Doe", 30, 50000.0);
54 emp.displayInfo();
55
56 // Modify attributes
57 emp.setName("Jane Doe");
58 emp.setAge(32);
59 emp.setSalary(55000.0);
60
61 System.out.println("
62Updated Information:");
63 emp.displayInfo();
64 }
65}
Output
Name: John Doe
Age: 30
Salary: $50000.0

Updated Information:
Name: Jane Doe
Age: 32
Salary: $55000.0

Summary

ConceptDescription
Accessing AttributesUse dot notation for public attributes; use getters for private attributes.
Modifying AttributesModify public attributes directly; use setters for private attributes.
Multiple AttributesManage multiple attributes using separate accessors and mutators.

What's Next?

Now that you understand how to manage class attributes, the next step is to learn about Java Class Methods. Methods define the behaviors of objects and are essential for creating interactive and functional Java applications. Continue your learning journey by exploring methods in Java classes.

Java Class Methods


PreviousJava Classes/ObjectsNext Java Class Methods

Recommended Gear

Java Classes/ObjectsJava Class Methods