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
A list of cool websites you might now know about
A thread 🧵
1) Learn Anything - Search tools for knowledge discovery that helps you understand any topic through the most efficient
2) Grad Speeches - Discover the best commencement speeches.
This website is made by me
3) What does the Internet Think - Find out what the internet thinks about anything
4) https://t.co/vuhT6jVItx - Send notes that will self-destruct after being read.
A thread 🧵
1) Learn Anything - Search tools for knowledge discovery that helps you understand any topic through the most efficient
2) Grad Speeches - Discover the best commencement speeches.
This website is made by me
3) What does the Internet Think - Find out what the internet thinks about anything
4) https://t.co/vuhT6jVItx - Send notes that will self-destruct after being read.
The first area to focus on is diversity. This has become a dogma in the tech world, and despite the fact that tech is one of the most meritocratic industries in the world, there are constant efforts to promote diversity at the expense of fairness, merit and competency. Examples:
USC's Interactive Media & Games Division cancels all-star panel that included top-tier game developers who were invited to share their experiences with students. Why? Because there were no women on the
ElectronConf is a conf which chooses presenters based on blind auditions; the identity, gender, and race of the speaker is not known to the selection team. The results of that merit-based approach was an all-male panel. So they cancelled the conference.
Apple's head of diversity (a black woman) got in trouble for promoting a vision of diversity that is at odds with contemporary progressive dogma. (She left the company shortly after this
Also in the name of diversity, there is unabashed discrimination against men (especially white men) in tech, in both hiring policies and in other arenas. One such example is this, a developer workshop that specifically excluded men: https://t.co/N0SkH4hR35
USC's Interactive Media & Games Division cancels all-star panel that included top-tier game developers who were invited to share their experiences with students. Why? Because there were no women on the
ElectronConf is a conf which chooses presenters based on blind auditions; the identity, gender, and race of the speaker is not known to the selection team. The results of that merit-based approach was an all-male panel. So they cancelled the conference.
Apple's head of diversity (a black woman) got in trouble for promoting a vision of diversity that is at odds with contemporary progressive dogma. (She left the company shortly after this
Also in the name of diversity, there is unabashed discrimination against men (especially white men) in tech, in both hiring policies and in other arenas. One such example is this, a developer workshop that specifically excluded men: https://t.co/N0SkH4hR35
