Getters on immutable type

I am writing some immutable types in Java and wondering what to call access methods. The Java Bean specification says accessors must be getX or isX, but since the fields are final, there is no setter, and the fields are more like attributes than properties.

There, I would prefer to call getter methods like:

public T x()

but not

public T getX()

Pay attention to an example:

public int java.lang.String#length()

(which could have been indicated so early in the history of Java, so the agreement is where they are not already).

Immutable objects expose the means for creating modified versions of themselves using methods that I tried to name as a verb, and not MyObject MyObject#setX(), this should limit the risk of the user calling setX () and think that the object has changed. So: MyObject MyObject#shine(newShineLevel).

. , Rectangle.setUpperLeft()? Rectangle.adjustUpperLeft , .

, , , , Java.

+1
6

, , getX. , JSP "x.y" , getY().

+7

getX() . :

  • java.lang.Integer.getInteger()
  • java.lang.Boolean.getBoolean()

, , java.lang.String.length(), getX. , , .

, java beans , getters/seters getX setX.

+6

Java , , get set, public final T getX().

, length() String getLength(), , String , length.

+6

, , get , bean. , get . String.length(). (intValue(), doubleValue(), booleanValue(),...), (name() ordinal()) (size()), , , get -less. ( Java get , bean.)

: get, bean. , . , , .

, , , Eclipse :)

+4

"" , . , Jakara Commons BeanUtils. / , , , bean.

, , , , , , get/set.

0

get - , , .

Get , -, . .

size(), length() .. , Borland , get/set GUI- ( , , ).

intValue, doubleValue .. , -.

Now, all that said, if you really want to use x or getX, is your choice. getters and setters are no longer needed for most decent toolkits, instead they will use annotations - just be prepared for a few people to take a few extra seconds by typing “get” and then “ctrl-space” and not finding that they are after.

0
source

All Articles