Java中定义常量方法及建议(Class/Interface)
Class定义常量方法(推荐方法)
//final修饰符
public final class Constants {
//私有构造方法
private Constants() {}
public static final int ConstantA = 100;
public static final int ConstantB = 100;
......
}
采用“类.常量名”方法进行调用。需要私有化构造方法,避免创建该类的实例。同时不需让其他类继承该类。
如果多处需要访问工具类中定义的常量,可以通过静态导入(static import)机制,避免用类名来修饰常量名。
Interface定义常量方法
public interface Constants {
int ConstantA = 100;
int ConstantB = 100;
......
}
在interface中声明的字段,虚拟机在编译时自动加上public static final修饰符。使用方法一般是“接口.常量名”。也可以通过实现该接口,直接访问常量名,即常量接口模式。
常量接口:即接口中不包含任何方法,只包含静态的final域,每个域都导出一个常量。使用这些常量的类实现这个接口,以避免用类名来修饰常量名。
常量接口模式是对接口的不良使用。具体参考如下:
The constant interface pattern is a poor use of interfaces. That a class uses some constants internally is an implementation detail. Implementing a constant interface causes this implementation detail to leak into the class's exported API. It is of no consequence to the users of a class that the class implements a constant interface. In fact, it may even confuse them. Worse, it represents a commitment: if in a future release the class is modified so that it no longer needs to use the constants, it still must implement the interface to ensure binary compatibility. If a nonfinal class implements a constant interface, all of its subclasses will have their namespaces polluted by the constants in the interface.
There are several constant interfaces in the java platform libraries, such as java.io.ObjectStreamConstants. These interfaces should be regarded as anomalies and should not be emulated.
区别
上述两种方法对比,interface中定义常量方法生成的class文件比第一种方法的class文件更小, 且代码更简洁, 效率更高.
但是在java中会产生问题,主要是java的动态性,java中一些字段的引用可以在运行期动态进行。某些场景下,部分内容改变可只进行部分编译。具体例子参考文档Java Interface 是常量存放的最佳地点吗?
该文推荐使用Class定义常量,但采用private修饰符,通过get方法获取常量。这种方案可以保证java的动态性。
public class A{
private static final String name = "bright";
public static String getName(){
return name;
}
参考:
https://www.ibm.com/developerworks/cn/java/l-java-interface/index.html
https://www.jianshu.com/p/0affad4762ef
Java中定义常量方法及建议(Class/Interface)的更多相关文章
- Java中定义常量(Constant) 的几种方法
为了方便大家交流Spark大数据,浪尖建了微信群,目前人数过多,只能通过浪尖或者在群里的朋友拉入群.纯技术交流,偶有吹水,但是打广告,不提醒,直接踢出.有兴趣加浪尖微信. 常量使用目的 1,为什么要将 ...
- 如何在Java中定义常量(Constant)
原本引自 http://blog.csdn.net/autofei/article/details/6419460 /** * Method One */ interface ConstantInt ...
- 在Java中定义常量
方法一采用接口(Interface)的中变量默认为static final的特性. 方法二采用了Java 5.0中引入的Enum类型. 方法三采用了在普通类中使用static final修饰变量的方法 ...
- JAVA中定义常量的几种方式
1.最古老的 //未处理 public static final Integer PROCESS_STATUS_UNTREATED = 0; //已接收 public static final Int ...
- 在C++中定义常量的两种方法的比较
常量是定以后,在程序运行中不能被改变的标识符.C++中定义常量可以用#define .const 这两种方法.例如:#define PRICE 10 //定义单价常量10const int PRICE ...
- 【Java_基础】java中的常量池
1.java常量池的介绍 java中的常量池,通常指的是运行时常量池,它是方法区的一部分,一个jvm实例只有一个运行常量池,各线程间共享该运行常量池. java常量池简介:java常量池中保存了一份在 ...
- 千万不要误用 java 中的 HashCode 方法
刚才debug追堆栈的时候发现一个很奇怪的问题 我用IE8和Google的浏览器访问同一个地址 Action的 scope="session" 也设置了 而且两个浏览器提交的参数m ...
- Java中的toString()方法
Java中的toString()方法 目录 Java中的toString()方法 1. 对象的toString方法 2. 基本类型的toString方法 3. 数组的toString ...
- Java中的main()方法详解
在Java中,main()方法是Java应用程序的入口方法,也就是说,程序在运行的时候,第一个执行的方法就是main()方法,这个方法和其他的方法有很大的不同,比如方法的名字必须是main,方法必须是 ...
随机推荐
- ip访问网站和localhost访问网站中top使用
对于相对定位,使用margin-top不用简单使用top. top在localhost中能正常显示,在ip访问时会出现多余空白. margin-top不管是localhost中还是ip中都能正常显示.
- mysql配置为半同步复制
mysql 半同步插件是由谷歌提供,具体位置/usr/local/mysql/lib/plugin/下,一个是 master用的 semisync_master.so,一个是 slave 用的 sem ...
- playbook role应用
参考: ansible中文权威指南 1. 动态Include 结合when等判断,在满足某个条件的时候加载. - include: test.yml when: ...... handler 中也可以 ...
- HDU2586How far away? LCA
去博客园看该题解 题意 给出一棵树,以及每条边的权值,给出一些询问,每个询问是2个节点,求每个询问对应的2个节点的距离. 算法 LCA_Tarjan 代码 #include <cstring&g ...
- 009 pandas的Series
一:创建 1.通过Numpy数组创建 2.属性查看 3.一维数组创建(与numpy的创建一样) 4.通过字典创建 二:应用Numpy数组运算 1.获取值 numpy的数组运算,在Series中都被保留 ...
- day 35 协程与gil概念
博客链接: http://www.cnblogs.com/linhaifeng/articles/7429894.html 今日概要: 1 生产者消费者模型(补充) 2 GIL(进程与线程的应用场景) ...
- Qt Creator插件Todo
转载:http://techieliang.com/2017/12/502/ 文章目录 1. 插件启动 2. 插件效果 3. 插件配置 4. 使用 码代码的过程往往并不是一气呵成,有时候需要记录 ...
- Django之setting文件
Django之setting文件 转载:https://www.jb51.net/article/128678.htm 目录 设置语言.时区 app路径 数据库配置 静态文件配置 中间件 sessio ...
- SpringSecurity整合JWT
一.前言 最近负责支付宝小程序后端项目设计,这里主要分享一下用户会话.接口鉴权的设计.参考过微信小程序后端的设计,会话需要依靠redis.相关的开发人员和我说依靠Redis并不是很靠谱,redis在业 ...
- ssh框架中.xml文件小技巧分离xml
struts.xml文件 struts.xml文件里的action可以分离出来,如: <!-- 预警信息监测 --> <include file="config/strut ...