Java conventions for accessible data (public visitors and recipients / naming)

Through the Java API, you can see countless conflicting naming and practices, which is really confusing to me

For example:

The string class has a private variable (integer) that tracks the size of the string in the name of count, but this is returned by a getter in the name of length ()

If you move to any type of array instead of the length of a getter method, they just pass variables through a public accessor, which can be through arrayinstance Length is obtained

Back to the string class, we have the string#getbytes () method, which is a getter, similar to the long () getter, but performs slightly more logic to obtain and return values

Personally, it seems superfluous for me to create a getter with the prefix get. For example, I prefer to enter gamepacket #data () and gamepacket #getdata (), but I think there may be a deeper meaning behind the naming, rather than just inconsistency

In addition, why does array [] not use the length of getters?

Can anyone explain a little light to me?

Solution

Getters (and setters) are from the Java Bean specification The reasons for using them are multiple:

>Most Java developers want visitors to name > APIs that follow these conventions easier to find For example, in my IDE, I often press Ctrl to find all the available information in the object. > Many APIs and frameworks rely on these conventions: JSP, El, MVC framework, bean filling from request parameters, JPA, dependency injection framework, such as spring, etc

You usually name getters in the same way as private variables that hold information, but the important thing is encapsulation and public APIs, so nothing can prevent you from calculating values in getters or naming private fields in different ways

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
分享
二维码
< <上一篇
下一篇>>