Naming conventions for java methods that return boolean(No question mark)

前端 未结 7 873
深忆病人
深忆病人 2020-12-02 10:49

I like using question mark at the end of method/function names in other languages. Java doesn\'t let me do this. As a workaround how else can I name boolean returning method

7条回答
  •  执笔经年
    2020-12-02 11:34

    If you wish your class to be compatible with the Java Beans specification, so that tools utilizing reflection (e.g. JavaBuilders, JGoodies Binding) can recognize boolean getters, either use getXXXX() or isXXXX() as a method name. From the Java Beans spec:

    8.3.2 Boolean properties

    In addition, for boolean properties, we allow a getter method to match the pattern:

    public boolean is<PropertyName>();

    This “is<PropertyName>” method may be provided instead of a “get<PropertyName>” method, or it may be provided in addition to a “get<PropertyName>” method. In either case, if the “is<PropertyName>” method is present for a boolean property then we will use the “is<PropertyName>” method to read the property value. An example boolean property might be:

    public boolean isMarsupial();
    public void setMarsupial(boolean m);
    

提交回复
热议问题