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
You May Also Like
THIS.
Russia hasn't been a willing partner in this treaty for almost 3 decades. We should have ended the pretense long ago.
Naturally, Rand Paul is telling anyone who will listen to him that Trump is making a HUGE MISTAKE here.
Rand is just like his dad, Ron. 100% isolationist.
They've never grasped that 100% isolationist is not 'America First' when you examine it. It really means 'America Alone'.
The consistent grousing of pursuing military alliances with allies - like Trump is doing now with Saudi Arabia.
So of course Rand has also spent the last 2 days loudly calling for Trump to kill the arms deal with Saudi Arabia and end our alliance with them.
What Obama was engineering with his foreign policy was de facto isolationism: pull all the troops out of the ME, abandon the region to Iranian control as a client state of Russia.
Obama wasn't building an alliance with Iran; he was facilitating abandoning the ME to Iran.
Obama wouldn't even leave behind a token security force, so of course what happened was the rise of ISIS. He also pumped billions of dollars into the Iranian coffers, which the Mullah's used to fund destabilizing activity [wars/terrorism] & criminal enterprises all over the globe
Russia hasn't been a willing partner in this treaty for almost 3 decades. We should have ended the pretense long ago.
Naturally, Rand Paul is telling anyone who will listen to him that Trump is making a HUGE MISTAKE here.
Arms control agreements are good when you have willing partners. Lightens the load on our military.
— John Noonan (@noonanjo) October 20, 2018
Russia hasnt been a willing partner in years. There will be gnashing of teeth from people who do arms control advocacy full time, but this is right movehttps://t.co/WmQE43ERCB
Rand is just like his dad, Ron. 100% isolationist.
They've never grasped that 100% isolationist is not 'America First' when you examine it. It really means 'America Alone'.
The consistent grousing of pursuing military alliances with allies - like Trump is doing now with Saudi Arabia.
So of course Rand has also spent the last 2 days loudly calling for Trump to kill the arms deal with Saudi Arabia and end our alliance with them.
What Obama was engineering with his foreign policy was de facto isolationism: pull all the troops out of the ME, abandon the region to Iranian control as a client state of Russia.
Obama wasn't building an alliance with Iran; he was facilitating abandoning the ME to Iran.
Obama wouldn't even leave behind a token security force, so of course what happened was the rise of ISIS. He also pumped billions of dollars into the Iranian coffers, which the Mullah's used to fund destabilizing activity [wars/terrorism] & criminal enterprises all over the globe