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,方法必须是 ...
随机推荐
- sed 简单修改配置文件ip地址
sed -i 's/old ip/new ip/g' file.txt
- scrapy笔记
1.关于请求url状态码重定向问题: from scrapy import Request handle_httpstatus_list = [404, 403, 500, 503, 521, 522 ...
- Ubuntu学习
一. Ubuntu简介 Ubuntu(乌班图)是一个基于Debian的以桌面应用为主的Linux操作系统,据说其名称来自非洲南部祖鲁语或科萨语的“ubuntu”一词,意思是“人性”.“我的存在是因为大 ...
- selenium_webdriver(python)控制浏览器滚动条
调过JS脚本控制 #coding=utf-8 from selenium import webdriver import time #访问百度 driver=webdriver.Firefox() d ...
- Codeforces 830C Bamboo Partition 其他
原文链接https://www.cnblogs.com/zhouzhendong/p/CF830C.html 题解 把问题转化成求最大的 $d$ ,满足$$\sum_{1\leq i \leq n}( ...
- 51Nod1626 B君的梦境 状压dp 矩阵
原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1626.html 题目传送门 - 51Nod1626 题意 题解 首先考虑形象的想象本题中的思维空间. ...
- Trident中的DRPC实现
一:介绍 1.说明 Storm里面引入DRPC主要是利用storm的实时计算能力来并行化CPU intensive的计算.DRPC的storm topology以函数的参数流作为输入,而把这些函数调用 ...
- Spark中map与flatMap
map将函数作用到数据集的每一个元素上,生成一个新的分布式的数据集(RDD)返回 map函数的源码: def map(self, f, preservesPartitioning=False): &q ...
- DFS Tempter of the Bone
http://acm.hdu.edu.cn/showproblem.php?pid=1010 用到了奇偶剪枝: 0 1 0 1 1 0 1 0 如图,设起点为s,终点为e,s-> ...
- VS2017 下载离线MSDN文档
VS2017 下载离线MSDN文档 点开帮助窗口的时候发现没有添加和删除帮助内容选项.处理方法如下: 1.打开vs2017安装包,如果你找不到安装包,可在相应你下载vs2017的浏览器上找到下载内容, ...