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

@franciscodeasis https://t.co/OuQaBRFPu7
Unfortunately the "This work includes the identification of viral sequences in bat samples, and has resulted in the isolation of three bat SARS-related coronaviruses that are now used as reagents to test therapeutics and vaccines." were BEFORE the


chimeric infectious clone grants were there.https://t.co/DAArwFkz6v is in 2017, Rs4231.
https://t.co/UgXygDjYbW is in 2016, RsSHC014 and RsWIV16.
https://t.co/krO69CsJ94 is in 2013, RsWIV1. notice that this is before the beginning of the project

starting in 2016. Also remember that they told about only 3 isolates/live viruses. RsSHC014 is a live infectious clone that is just as alive as those other "Isolates".

P.D. somehow is able to use funds that he have yet recieved yet, and send results and sequences from late 2019 back in time into 2015,2013 and 2016!

https://t.co/4wC7k1Lh54 Ref 3: Why ALL your pangolin samples were PCR negative? to avoid deep sequencing and accidentally reveal Paguma Larvata and Oryctolagus Cuniculus?
“We don’t negotiate salaries” is a negotiation tactic.

Always. No, your company is not an exception.

A tactic I don’t appreciate at all because of how unfairly it penalizes low-leverage, junior employees, and those loyal enough not to question it, but that’s negotiation for you after all. Weaponized information asymmetry.

Listen to Aditya


And by the way, you should never be worried that an offer would be withdrawn if you politely negotiate.

I have seen this happen *extremely* rarely, mostly to women, and anyway is a giant red flag. It suggests you probably didn’t want to work there.

You wish there was no negotiating so it would all be more fair? I feel you, but it’s not happening.

Instead, negotiate hard, use your privilege, and then go and share numbers with your underrepresented and underpaid colleagues. […]