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
And here they are...
THE WINNERS OF THE 24 HOUR STARTUP CHALLENGE
Remember, this money is just fun. If you launched a product (or even attempted a launch) - you did something worth MUCH more than $1,000.
#24hrstartup
The winners 👇
#10
Lattes For Change - Skip a latte and save a life.
https://t.co/M75RAirZzs
@frantzfries built a platform where you can see how skipping your morning latte could do for the world.
A great product for a great cause.
Congrats Chris on winning $250!
#9
Instaland - Create amazing landing pages for your followers.
https://t.co/5KkveJTAsy
A team project! @bpmct and @BaileyPumfleet built a tool for social media influencers to create simple "swipe up" landing pages for followers.
Really impressive for 24 hours. Congrats!
#8
SayHenlo - Chat without distractions
https://t.co/og0B7gmkW6
Built by @DaltonEdwards, it's a platform for combatting conversation overload. This product was also coded exclusively from an iPad 😲
Dalton is a beast. I'm so excited he placed in the top 10.
#7
CoderStory - Learn to code from developers across the globe!
https://t.co/86Ay6nF4AY
Built by @jesswallaceuk, the project is focused on highlighting the experience of developers and people learning to code.
I wish this existed when I learned to code! Congrats on $250!!
THE WINNERS OF THE 24 HOUR STARTUP CHALLENGE
Remember, this money is just fun. If you launched a product (or even attempted a launch) - you did something worth MUCH more than $1,000.
#24hrstartup
The winners 👇
#10
Lattes For Change - Skip a latte and save a life.
https://t.co/M75RAirZzs
@frantzfries built a platform where you can see how skipping your morning latte could do for the world.
A great product for a great cause.
Congrats Chris on winning $250!
#9
Instaland - Create amazing landing pages for your followers.
https://t.co/5KkveJTAsy
A team project! @bpmct and @BaileyPumfleet built a tool for social media influencers to create simple "swipe up" landing pages for followers.
Really impressive for 24 hours. Congrats!
#8
SayHenlo - Chat without distractions
https://t.co/og0B7gmkW6
Built by @DaltonEdwards, it's a platform for combatting conversation overload. This product was also coded exclusively from an iPad 😲
Dalton is a beast. I'm so excited he placed in the top 10.
#7
CoderStory - Learn to code from developers across the globe!
https://t.co/86Ay6nF4AY
Built by @jesswallaceuk, the project is focused on highlighting the experience of developers and people learning to code.
I wish this existed when I learned to code! Congrats on $250!!
Great article from @AsheSchow. I lived thru the 'Satanic Panic' of the 1980's/early 1990's asking myself "Has eveyrbody lost their GODDAMN MINDS?!"
The 3 big things that made the 1980's/early 1990's surreal for me.
1) Satanic Panic - satanism in the day cares ahhhh!
2) "Repressed memory" syndrome
3) Facilitated Communication [FC]
All 3 led to massive abuse.
"Therapists" -and I use the term to describe these quacks loosely - would hypnotize people & convince they they were 'reliving' past memories of Mom & Dad killing babies in Satanic rituals in the basement while they were growing up.
Other 'therapists' would badger kids until they invented stories about watching alligators eat babies dropped into a lake from a hot air balloon. Kids would deny anything happened for hours until the therapist 'broke through' and 'found' the 'truth'.
FC was a movement that started with the claim severely handicapped individuals were able to 'type' legible sentences & communicate if a 'helper' guided their hands over a keyboard.
For three years I have wanted to write an article on moral panics. I have collected anecdotes and similarities between today\u2019s moral panic and those of the past - particularly the Satanic Panic of the 80s.
— Ashe Schow (@AsheSchow) September 29, 2018
This is my finished product: https://t.co/otcM1uuUDk
The 3 big things that made the 1980's/early 1990's surreal for me.
1) Satanic Panic - satanism in the day cares ahhhh!
2) "Repressed memory" syndrome
3) Facilitated Communication [FC]
All 3 led to massive abuse.
"Therapists" -and I use the term to describe these quacks loosely - would hypnotize people & convince they they were 'reliving' past memories of Mom & Dad killing babies in Satanic rituals in the basement while they were growing up.
Other 'therapists' would badger kids until they invented stories about watching alligators eat babies dropped into a lake from a hot air balloon. Kids would deny anything happened for hours until the therapist 'broke through' and 'found' the 'truth'.
FC was a movement that started with the claim severely handicapped individuals were able to 'type' legible sentences & communicate if a 'helper' guided their hands over a keyboard.