Let's find out why do we following this practice.
Java: Why do we use getter and setter methods?
a thread...
Let's find out why do we following this practice.
As a common practice, both the variables are private and define the public getter and setter method to read and write their values.
Eg:

The public getter and setter method act as a single door to access the private fields.
Before updating the value we can run any validation in the setter method and accordingly allow field modification.
Eg:

Similar to Validation, we can also put any security-related code to secure our data inside the getter and setter.
For eg. Check if a user has access to the field based on our complex security logic and then allow the user to either read or update the value.
To allow only write permission, we can keep setter methods.
Similarly, to allow only read permission to fields, we can remove the setter method and only keep the getter method as shown below:

To create an immutable class, we can remove the setter and put-getter methods.
In getter methods, we can return a new copy instead of returning the original object to protect it from getting modified.

In the above scenarios, we've only achieved encapsulations at diff levels & that's the main reason for using getter/setter in java.
To see the above examples in more detail and run them you can access below git repo:
https://t.co/vHZsSmtJqS
More from Vikas Rajput
Learn Core Java, in 30 days as an Absolute Beginner
a roadmap...
Core Java can be Overwhelming, in the beginning!
Let's see what are those topics you should cover and what you can skip in Core Java.
1. Theoretical Concepts (2 days)
- Features of Java
- JVM, JRE & JDK
- Memory areas in Java
- Basics of how Garbage Collection Works.
2. OOP Concepts (3 days)
- Objects and Classes
- Inheritance
- Polymorphism
- Encapsulation
This topic forms the basis of your Java learning. Spent enough time on this topic and understanding it using real-world examples and some
3. Primitives data types, Operators & Arrays (3 days)
- Various primitives/non-primitives data types, operators
- What is their use, How to use them
- Various sizes of data types
- Arrays, practice a few of its problems
- 2D/3D arrays and their use cases, practice a few problems
a roadmap...
Core Java can be Overwhelming, in the beginning!
Let's see what are those topics you should cover and what you can skip in Core Java.
1. Theoretical Concepts (2 days)
- Features of Java
- JVM, JRE & JDK
- Memory areas in Java
- Basics of how Garbage Collection Works.
2. OOP Concepts (3 days)
- Objects and Classes
- Inheritance
- Polymorphism
- Encapsulation
This topic forms the basis of your Java learning. Spent enough time on this topic and understanding it using real-world examples and some
OOPs Concept in Java
— Vikas Rajput (@vikasrajputin) April 4, 2022
a thread...
3. Primitives data types, Operators & Arrays (3 days)
- Various primitives/non-primitives data types, operators
- What is their use, How to use them
- Various sizes of data types
- Arrays, practice a few of its problems
- 2D/3D arrays and their use cases, practice a few problems
You May Also Like
Tip from the Monkey
Pangolins, September 2019 and PLA are the key to this mystery
Stay Tuned!
1. Yang
2. A jacobin capuchin dangling a flagellin pangolin on a javelin while playing a mandolin and strangling a mannequin on a paladin's palanquin, said Saladin
More to come tomorrow!
3. Yigang Tong
https://t.co/CYtqYorhzH
Archived: https://t.co/ncz5ruwE2W
4. YT Interview
Some bats & pangolins carry viruses related with SARS-CoV-2, found in SE Asia and in Yunnan, & the pangolins carrying SARS-CoV-2 related viruses were smuggled from SE Asia, so there is a possibility that SARS-CoV-2 were coming from
Pangolins, September 2019 and PLA are the key to this mystery
Stay Tuned!

1. Yang
Meet Yang Ruifu, CCP's biological weapons expert https://t.co/JjB9TLEO95 via @Gnews202064
— Billy Bostickson \U0001f3f4\U0001f441&\U0001f441 \U0001f193 (@BillyBostickson) October 11, 2020
Interesting expose of China's top bioweapons expert who oversaw fake pangolin research
Paper 1: https://t.co/TrXESKLYmJ
Paper 2:https://t.co/9LSJTNCn3l
Pangolinhttps://t.co/2FUAzWyOcv pic.twitter.com/I2QMXgnkBJ
2. A jacobin capuchin dangling a flagellin pangolin on a javelin while playing a mandolin and strangling a mannequin on a paladin's palanquin, said Saladin
More to come tomorrow!

3. Yigang Tong
https://t.co/CYtqYorhzH
Archived: https://t.co/ncz5ruwE2W

4. YT Interview
Some bats & pangolins carry viruses related with SARS-CoV-2, found in SE Asia and in Yunnan, & the pangolins carrying SARS-CoV-2 related viruses were smuggled from SE Asia, so there is a possibility that SARS-CoV-2 were coming from