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.
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:
Attributes can be accessed using the dot notation (.) on an object instance. The access depends on the attribute's visibility (public, private, protected).
1class Car {2public String color;3public int year;45public Car(String color, int year) {6this.color = color;7this.year = year;8};9}1011public class Main {12public static void main(String[] args) {13Car myCar = new Car("Red", 2020);14System.out.println("Color: " + myCar.color);15System.out.println("Year: " + myCar.year);16}17}
Color: Red Year: 2020
Private attributes cannot be accessed directly from outside the class. They require public getter methods.
1class Car {2private String color;3private int year;45public Car(String color, int year) {6this.color = color;7this.year = year;8}910public String getColor() {11return color;12}1314public int getYear() {15return year;16}17}1819public class Main {20public static void main(String[] args) {21Car myCar = new Car("Blue", 2018);22System.out.println("Color: " + myCar.getColor());23System.out.println("Year: " + myCar.getYear());24}25}
Color: Blue Year: 2018
Attributes can be modified using setter methods or directly (for public attributes).
1class Car {2public String color;3public int year;45public Car(String color, int year) {6this.color = color;7this.year = year;8}9}1011public class Main {12public static void main(String[] args) {13Car myCar = new Car("Green", 2019);14System.out.println("Original Color: " + myCar.color);1516// Modify the attribute17myCar.color = "Yellow";18System.out.println("Modified Color: " + myCar.color);19}20}
Original Color: Green Modified Color: Yellow
Private attributes should be modified using setter methods.
1class Car {2private String color;3private int year;45public Car(String color, int year) {6this.color = color;7this.year = year;8}910public void setColor(String color) {11this.color = color;12}1314public void setYear(int year) {15this.year = year;16}17}1819public class Main {20public static void main(String[] args) {21Car myCar = new Car("Black", 2017);22System.out.println("Original Color: " + myCar.color);2324// Modify the attribute using setter25myCar.setColor("White");26System.out.println("Modified Color: " + myCar.color);27}28}
Original Color: Black Modified Color: White
A class can have multiple attributes, each serving a different purpose. Managing these attributes effectively is crucial for maintaining clean and efficient code.
1class Car {2private String make;3private String model;4private int year;56public Car(String make, String model, int year) {7this.make = make;8this.model = model;9this.year = year;10}1112public String getMake() {13return make;14}1516public void setMake(String make) {17this.make = make;18}1920public String getModel() {21return model;22}2324public void setModel(String model) {25this.model = model;26}2728public int getYear() {29return year;30}3132public void setYear(int year) {33this.year = year;34}35}3637public class Main {38public static void main(String[] args) {39Car myCar = new Car("Toyota", "Corolla", 2021);40System.out.println("Make: " + myCar.getMake());41System.out.println("Model: " + myCar.getModel());42System.out.println("Year: " + myCar.getYear());4344// Modify attributes45myCar.setMake("Honda");46myCar.setModel("Civic");47myCar.setYear(2022);4849System.out.println("50Updated Details:");51System.out.println("Make: " + myCar.getMake());52System.out.println("Model: " + myCar.getModel());53System.out.println("Year: " + myCar.getYear());54}55}
Make: Toyota Model: Corolla Year: 2021 Updated Details: Make: Honda Model: Civic Year: 2022
Let's create a practical example that demonstrates the use of multiple attributes, accessors, and mutators.
1class Employee {2private String name;3private int age;4private double salary;56public Employee(String name, int age, double salary) {7this.name = name;8this.age = age;9this.salary = salary;10}1112public String getName() {13return name;14}1516public void setName(String name) {17this.name = name;18}1920public int getAge() {21return age;22}2324public void setAge(int age) {25if (age > 0) {26this.age = age;27} else {28System.out.println("Invalid age");29}30}3132public double getSalary() {33return salary;34}3536public void setSalary(double salary) {37if (salary >= 0) {38this.salary = salary;39} else {40System.out.println("Invalid salary");41}42}4344public void displayInfo() {45System.out.println("Name: " + name);46System.out.println("Age: " + age);47System.out.println("Salary: $" + salary);48}49}5051public class Main {52public static void main(String[] args) {53Employee emp = new Employee("John Doe", 30, 50000.0);54emp.displayInfo();5556// Modify attributes57emp.setName("Jane Doe");58emp.setAge(32);59emp.setSalary(55000.0);6061System.out.println("62Updated Information:");63emp.displayInfo();64}65}
Name: John Doe Age: 30 Salary: $50000.0 Updated Information: Name: Jane Doe Age: 32 Salary: $55000.0
| Concept | Description |
|---|---|
| Accessing Attributes | Use dot notation for public attributes; use getters for private attributes. |
| Modifying Attributes | Modify public attributes directly; use setters for private attributes. |
| Multiple Attributes | Manage multiple attributes using separate accessors and mutators. |
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.