Java: Why do we use getter and setter methods?

a thread...

In Java classes, we normally create the getter and setter methods to read and update class level fields respectively.

Let's find out why do we following this practice.
Consider a class "Account", having fields like accountName and accountBalance - to show the name and balance of the account.

As a common practice, both the variables are private and define the public getter and setter method to read and write their values.

Eg:
Using the above example, let's see different use-cases where having getter and setter methods can be game-changing.
1. Validation:

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:
2. Security

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.
3. ReadOnly or WriteOnly Permission

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:
4. Immutability:

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.
Conclusion:

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