Java – when do I create a getter method without “get” in its name?

The following questions have bothered me for a long time. I can't find any information about naming conventions

There are methods, such as:

java.util.Collection.size();

This method is actually a getter of the size of a collection, but it does not start with "get" like the conventional getter method

So what is the naming convention for these types of methods?

Solution

Collection. Size () is not a typical getter because it is not a simple field return - it is the size of a collection, so you don't have SetSize () because it's not the field you set - it's a "calculated" value

You can think like this - getters and setters are usually used together to encapsulate a field - if you can't do setx (), it's not a typical getter / setter

But this is just a matter of convention. Some people believe that it is always used without getting or setting it

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>