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条回答
在下西门庆
2楼-- · 2019-01-12 05:27

There are several reasons:

  • Some Java APIs rely on them (e.g. Servlet API);
  • making non-final variable public is considered to be a bad style;
  • further code support: if sometime in future you`ll need to perform some actions before each access/mutation (get/set) of the variable, you will have less problems with it. In C# constructions like

    public int Age { get { return (int)(today() - m_BirthDate); } }

are are just syntactic sugar.

查看更多
Viruses.
3楼-- · 2019-01-12 05:28

This is done so you can change the getter or setter implementation in your public API after you release it. Using public fields, you wouldn't be able to check values for validity.

查看更多
再贱就再见
4楼-- · 2019-01-12 05:32

Because interfaces only allow for specifying methods, not variables. Interfaces are the building stones of API's.

Hence, to access a field through an interface, you need to have the getter and setter.

查看更多
老娘就宠你
5楼-- · 2019-01-12 05:32

The most and foremost use for getters and setters in Java is to annoy the developers. The second most important use is to clutter the code with useless noise. Additionally, it forces you to use a different name for the same thing, depending on where you are (inside or outside the class). Not to forget the added ambiguity (do you call the getter inside the class or do you use the field directly?) Next, they are used to allow access to private data but that's just a minor side effect ;)

In other programming languages, the compiler will generate them for you (unless, of course, you provide your own implementations). In Delphi, for example, you have read and write modifiers for fields (just like private, static or final in Java). The define if you'll have a getter or setter generated for you.

Unlike the Delphi guys, the Java guys wanted everything to be explicit. "If it's not in the source, it's not there". So the only solution was to force people to write all the getters and setters manually. Even worse, people have to use a different name for the same thing.

查看更多
贪生不怕死
6楼-- · 2019-01-12 05:32

Having a setter allows you

  • perform validation
  • to fire a property changed event if the new value is different from the previous value

In the case in question there is no need for getter and setter if the value is simply read or written.

查看更多
爷的心禁止访问
7楼-- · 2019-01-12 05:37

property idea is core in OOP (Object oriented programming). But problem is that Java introduce them not in core of language (syntax / JVM), but (probably few years later??? historics of Java say better) as convention: pair of consistent getters/setter is property in bean, concept of property is in libraries, not in core.

This generate problem in few libraries, framework. Is single getter a read only property or not? That is the question. I.e.in JPA entities if You want implement classic method (algorithm) beggining with "get" like getCurrentTine() is the best mark by @Transient to disable interpretation like property having value.

In other words, I like very much property concept in C# designed 10 years later and better. BTW C# property has getter/setter too, but sometimes/partially hidden, visible at low level debugging. Free from question "why getter" etc ...

In Java world is interesting to read about Groovy concept of property (hidden getter/setter in different way than C#) http://www.groovy-lang.org/objectorientation.html#_fields_and_properties

EDIT: from real life, every java object has getClass() method, tools from java.beans.BeanInfo package report this as property "class", but this not true. It isn't property (readonly property) in full sense. I imagine properties like C# (with his internal hidden name get_Something1) hasn't conflict with "functional" GetSomething2()

查看更多
登录 后发表回答