Domanda

Had a bit of a "is this real life moment". Why are we naming mutators with get and set prefixes:

It would just as easy and understandable to just do

myMember(myMember Member)

as setMyMember and getMyMember

Is there a historical reason why Java has this style?

È stato utile?

Soluzione

The get* and set* style of naming is specified in the JavaBeans specification, and some libraries using reflection expect this style to be used. For example, Jackson mapper can serialize a java object using the get/setters to JSON without any additional annotations; if you use a different naming style, you have to tell it where your properties are.

Note that some other programming languages use different styles. Perl libraries, for example, often use a ->someProperty() getter and ->someProperty($newValue) setter.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top