Why should I write Getter/Setter?

Why should I write Getter/Setter?

When I started my career in Java, I was confused about getter and setter. One question always comes to my mind why should I write getter/setter? It looks weird kind of syntax to me.

I learned that by public access modifier one field of a class is accessible to any packages, and by Getter/setter what I am doing is same, make the field private and public the getter/setter method, so it can be accessed by any packages.

So, What is the difference between following two expressions?


public String name=”Shamik”;

Caller
========

String name = X.name;   //(X is a object instance);
X.name=”Shamik Mitra”;
private String name=”Shamik”;

public String getName(){
return name;}

public void setName(String name){
this.name=name;}

Caller
=====
String name = X.getname();
X.setName(“Shamik Mitra”);


confused.jpg
What the heck is getter/setter?




Slowly I realize why we use getter/setter & why it is important.

In this Article, I share that realization.

Realization :

The main difference of making a field public VS expose it through getter/setter is to hold the control to the property.If you make a field  public that means you provide direct access to the caller, caller can do anything with your filed knowingly/unknowingly say, caller can send a null value and if you use that filed in another method it may blow up that method by null pointer exception,

But If you provide getter/setter, you provide them an indirect access while taking full control, because the only way to set a value through setter and get a value through getter, So now you have exact one entry and one exit point of your field and getter/setters are methods!!! (Which allows block of codes) so you can do validation check on them and takes the decision you should set the caller value or not, same in getter method you can take decision should you return the actual reference or clone it and return the same to the caller.


So, getter/Setter are work as fuse or circuit breaker where the current has to be passed through fuse if anything goes abnormal fuse detached from the main circuit, so the circuit is safe. The concept is same here if anything goes wrong setter will not pass the value to class member field.

After reading the explanation, I know still you have one question

I understand all but generally, we do not write anything in getter/setter just return the field and set the field, which is same as exposing a field as public so why are you saying all of this?

To answer this question I say writing getter/setter we create a provision to add any validation method in future, currently, there is no validation but if anything goes wrong in future we just add validation logic in the setter.

But still, it creates a debate who are big follower of YAGNI(You aren't gonna need it),
they can say when there are no such validation constraints for a field why should I write getter/setter, I only expose it as public.

As per my understanding, The crux of YAGNI is to Unnecessary not make your code complex, As someone like to think big and try to make their code base so generic that it welcome any changes but most of the changes he/she thinks will never come.

Conclusion: The  getter/setter does not make your code complex and welcome future validations. So Please go for it blindly.