拥有一个仅包含公共静态最终字段的 Java 类有什么好处?
我正在审查另一位员工的工作中的一些代码,并发现一个仅包含公共静态最终字段的类。这样做有什么好处,如何使用?我的猜测是,它可以轻松地从 XML 标记中检索信息。还有其他想法或知识吗?
I'm reviewing some code from my job from another employee and came across a class consisting of only public static final fields. What is the benefit of this, and how would it be used? My guess is that it makes it easy to retrieve info from XML tags. Any other ideas or knowledge?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
定义(某种)常量是一种常见的 Java 习惯用法,以避免在代码中对固定值(“魔法”——可能重复且难以重构)进行编码。
https://softwareengineering.stackexchange.com/questions/59642/best-practices-for-constants< /a>
http://www.javapractices.com/topic/TopicAction.do ?Id=2
Java 枚举与静态常量
涉及幻数的全局常量的最佳实践
It's a common Java idiom to define (sort of) constants, to avoid harcoding fixed ('magic' - probably duplicated and hard to refactor) values in code.
https://softwareengineering.stackexchange.com/questions/59642/best-practices-for-constants
http://www.javapractices.com/topic/TopicAction.do?Id=2
Java enumerations vs. static constants
Best practice for global constants involving magic numbers
在 Java 中,出于某种原因拥有一组常量值是一种常见(尽管不好)的方式。另外,如果是较旧的 Java 代码,那么在语言支持之前,这是实现枚举的常见方法
Its a common (albeit bad) way in Java to have a collection of constant values for one reason or another. Also, if its older Java code, it was a common way to implement enums before there was language support for them
一般我们用它来保存常量值。
例如:
另请参见
Generally we use it to hold constants value.
For example:
Also See
它用于保存单个/多个源文件所需的常量。
但为了实现这一点,我宁愿使用接口,而不是类。由于任何课程都可以实现它,因此它将对我的目的有更多帮助。尽管这个选择取决于需求。
It is used for holding constants which will be required by single/multiple source files.
But to achieve this, I would rather use
interface
, instead of class. Since any class can implement it, it will help more in my purpose. Although this choice depends on requirements.