What are the uses of getter/setters in Java? [dupl

2019-01-12 05:08发布

This question already has an answer here:

I have seen member variables given a private modifier and then using getter/setter methods just to set and get the values of the variable (in the name of standardization).

Why not then make the variable public itself (Other than cases like spring framework which depends on getter/setters for IOC etc). It serves the purpose.

In C# I have seen getter/setter with Capitalization of the member variable. Why not make the variable public itself?

15条回答
Anthone
2楼-- · 2019-01-12 05:13

It's part of encapsulation: abstracting a class's interface (the "getters" and "setters") from its implementation (using an instance variable). While you might decide to implement the behaviour through direct access to an instance variable today, you might want to do it differently tomorrow. Say you need to retrieve the value over the network instead of storing it locally—if you have encapsulated the behaviour, that's a trivial change. If other objects are relying on direct access to an instance variable, though, you're stuck.

查看更多
劳资没心,怎么记你
3楼-- · 2019-01-12 05:16

Getters and setters may very well be the greatest lie ever told. They are considered a sign of good design, while the opposite is true. New programmers should be taught proper encapsulation, not to write dumb data carrier classes that contain nothing but getters and setters.

(The idea that you need getters and setters to future-proof your code if you want to change the implementation later on is an obvious case of YAGNI. But that is really beside the point.)

查看更多
放荡不羁爱自由
4楼-- · 2019-01-12 05:18

The most common reason is a poor understanding of encapsulation. When the developer believes that encapsulating stuff really just means getters & setters rather than encapsulating behavour.

The valid reasons for having getters/setters are:

1) You are making a generic¹ object such as JComponent. By using a getter/setter rather than direct access to the variable means that you can do some pre-processing on said variable first (such as validate it is with a set range) or change the underlying implementation (switching from an int to a BigInteger without changing the public API).

2) Your DI framework does not support ctor injection. By having just a setter you can ensure that the variable is only set once.

3) (Ties in with #1) To allow tools to interact with your object. By using such a simple convention then GUI tools can easily get all the settings for a given component. An example of this would be the UI builder in NetBeans.

¹ Of the not-Generic type. Bad word to use I know, please suggest an alternative.

查看更多
SAY GOODBYE
5楼-- · 2019-01-12 05:20

Some Java frameworks require them (JavaBeans I think).

-- Edit

Some posters are trying to say this is about encapsulation. It isn't.

Encapsulation is about hiding the implementation details of your object, and exposing only relevant functions.

Providing a get/set that does nothing but set a value does not accomplish this at all, and the only reason for them is:

  • Perform some additional validation before set/get
  • Get the variable from somewhere else
  • Integrate with frameworks (EJB)
查看更多
手持菜刀,她持情操
6楼-- · 2019-01-12 05:24

Even back in 2003 it was known that getter and setter methods are evil.

查看更多
不美不萌又怎样
7楼-- · 2019-01-12 05:25

Well,

OOP. ;)

Or to be a little more precise:

Getters and Setters are used to provide a defined interface to a classes properties. Check the OOP link, it describes the concepts more in detail...

K

查看更多
登录 后发表回答