Why should we use setter and getter methods over public variables in a java class ?

This is debatable and the one the imp and confusion question and asked in forums that "In java class if we have public variables available then why we need to use setters and getters" . Sometimes interviewer asked this question and we used to get confuse over that .Here in this post I would try to clear all doubts on the battle between setter and getters vs public variables .

First we need to understand about public variables and setters and getters . So without going in-depth on this, public variables in java class are having scope as public that means there is no restriction on that , anyone can create object of that class and access those variables value. If any class is extending this class then the subclass and also use these variables without any restrictions'.

Now coming on setters and getters so they are simply methods which are being used for setting and getting the variables value from class .
Now we have question like if we are already having public variables then why we need these extra methods .I will clear this doubt in the following points.

Note :- the following points are majorly based in the sense of whole project or big application not just for one or two classes.

1. The Java Beans standard:- suggests using setters and getters over public variables it says we should declare all the variables in a class as private then we need to use setters and getters to get those variables .

2. Encapsulation:- Using setters and getters with private variables follows encapsulation principle. It helps in hiding the internal representation of variable by exposing using an alternative methods.

3. Validation:- If we simply want to access data from variables then we can use public variables instead of getters , but if want to do some verification or want to execute some conditions on that variables then its not possible in case of public variables then our setters and getters are good in this case.
As are prone to do errors now assume if someone accidently puts a variable value as null then it will throw NPE while doing any operation on it .This this we can prevent it by using a condition our setter method .

4. Maintainability:- Imagine you have used a public variable in 100 classes now the requirement comes to validate some condition over it then we need to change the code in all 100 places that is very horrible idea that why its good not to use public variables .

5. Industry Standards :- Some libraries follows beans standards in class that they works on setter and getters .

6. Debugging:- -It also helps in debugging of code , since we can place debug point over setter and getters and easily debug value instead of placing debug point on every variable location .


Conclusion :- Many people may argue like why to increase code or if we are simply going to retrieve value they why not use public variables, then its totally based on your requirements .Yes sometimes it violets YAGNI principle but its welcomes future validations .

So if you are thinking writing setters and getters is too much then now a days many ide generates these for you in the class or you can go for project lombok which is now a days being used in almost every project.


Thanks.