Logan17
JCHJava Community | Help. Code. Learn.
•Created by Abdullah on 9/22/2024 in #java-help
GETTERS AND SETTERS
Ex.2
7 replies
JCHJava Community | Help. Code. Learn.
•Created by Abdullah on 9/22/2024 in #java-help
GETTERS AND SETTERS
Ex.
7 replies
JCHJava Community | Help. Code. Learn.
•Created by Abdullah on 9/22/2024 in #java-help
GETTERS AND SETTERS
It's literally what their names say.
Getters and setters are methods that allow controlled access to a class's attributes. A getter retrieves the value of an attribute, while a setter modifies it.
For example, if you have a private attribute that you don't want to expose directly to other classes, you can create a public getter that allows access to that value. This way, other classes can retrieve the value without directly accessing the attribute. Similarly, if you need to change the value of that private attribute, you can use a setter, which allows modification without exposing the attribute directly.
This approach helps maintain encapsulation and data integrity within your class.
7 replies