Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of getters and setters? [duplicate]

Possible Duplicate:
Why use getters and setters?

I have read books on Java, saying that it is good to create setters and getters for variables such as x and y. For example:

public int getX(){     return x; }  public void setX(int x){     this.x = x; } 

But what is the difference from that and

...(shape.x)...   // Basically getX() 

and

shape.x = 90;    // Basically setX() 

If setters and getters are better, what practical problems would arise?

like image 441
Anonymous181 Avatar asked May 02 '12 04:05

Anonymous181


People also ask

What is the point of getter and setter methods?

Getters and setters are used to protect your data, particularly when creating classes. For each instance variable, a getter method returns its value while a setter method sets or updates its value. Given this, getters and setters are also known as accessors and mutators, respectively.

Are getters and setters bad practice?

Getter and setter methods (also known as accessors) are dangerous for the same reason that public fields are dangerous: They provide external access to implementation details. What if you need to change the accessed field's type? You also have to change the accessor's return type.

What is the advantage of using getters and setters that only get and set instead of simply using public fields for those variables?

1) getter and setter method gives you centralized control on how a the particular field is initialized and provided to the client which makes the validation and debugging much easier. you can simply put breakpoints or print statement to see which thread are accessing and what values are going out.

Are setters and getters necessary?

Introduction. Getters and Setters play an important role in retrieving and updating the value of a variable outside the encapsulating class. A setter updates the value of a variable, while a getter reads the value of a variable.


2 Answers

Multiple reasons:

  • If you allow field access like

    shape.x = 90

then you cannot add any logic in future to validate the data.

say if x cannot be less than 100 you cannot do it, however if you had setters like

public void setShapeValue(int shapeValue){   if(shapeValue < 100){     //do something here like throw exception.   } } 
  • You cannot add something like copy on write logic (see CopyOnWriteArrayList)
  • Another reason is for accessing fields outside your class you will have to mark them public, protected or default, and thus you loose control. When data is very much internal to the class breaking Encapsulation and in general OOPS methodology.

Though for constants like

public final String SOMETHING = "SOMETHING"; 

you will allow field access as they cannot be changed, for instance variable you will place them with getters, setters.

  • Another scenario is when you want your Class to be immutable, if you allow field access then you are breaking the immutability of your class since values can be changed. But if you carefully design your class with getters and no setters you keep the immutability intact.

Though in such cases you have to be careful in getter method to ensure you don't give out reference of objects(in case your class have object as instances).

We can use the private variables in any package using getters and setters.

like image 141
mprabhat Avatar answered Oct 13 '22 06:10

mprabhat


Using getter and setter functions allow for constraints and encapsulation. Lets say x is the radius. shape.x = -10 would not make much sense. Also, if someone tries to set an illegal value, you can print an error, set a default value, or do nothing.

It is good practice to make member variables private so they cannot be modified directly by programs using them.

Mutator functions
Encapsulation

like image 37
JustinDanielson Avatar answered Oct 13 '22 06:10

JustinDanielson