一 静态内部类,调用的时候才加载,节约内存!
package com.del.tools; public final class Flag { private Flag(){} public static final class Rca{ private Rca(){} public static final String NAME = "zhangsan"; public static final String HOEM = "zhangsan"; } public static final class Fm{ private Fm(){} public static final String HOEM = "zhangsan"; public static final String HAJE = "zhangsan"; } }
获取Hg模块的姓名 : String name = Flage.Hg.NAME;
二 在接口里定义静态内部类,可以把不同的功能的常量类进一步分类,比直接在接口里面定义常量要好!(但是不建议使用接口定义常量,详见http://hussion.iteye.com/blog/1322308,http://stackoverflow.com/questions/2659593/what-is-the-use-of-interface-constants)
public interface UtilConstants { /** * * 公共常量 * */ public static final class Public { public static final String ID = "TESTID"; } /** * JSP路径 */ public static finalclass JspFilePath { public static final String TESTCONTROLLER = "jsp/basic/"; public static final String TEMPLATE_PAGEPATH = "basic/template/"; // 模板(测试) } /** * vo 对象的一些公共的属性名称 * */ public static final class VoFields { public static final String ACTIONTIME = "operateTime";//操作时间 public static final String ACTIONUSERNAME = "operatorName";//操作人姓名 public static final String CHECKTIME = "auditTime";//审核时间 public static final String CHECKUSERID = "checkUserId";//审核人ID public static final String CHECKUSERNAME = "auditPerson";//审核人姓名 public static final String CREATETIME = "createTime"; // 创建时间 public static final String CREATEUSERID = "createUserId";// 创建人code public static final String INSERTUSERNAME = "createUserName";// 创建人姓名 public static final String UPDATETIME = "updateTime"; // 修改时间 public static final String UPDATEUSERID = "updateUserId";// 修改人CODE public static final String UPDATEUSERNAME = "updateUserName";// 修改人姓名 public static final String DELFLAG = "delFlag"; // 删除标记 public static final String DBID = "dbid"; // 主键 } }
获取ID属性: String id = UtilConstants.Public.ID;
三 枚举常量类
package com.del.tools; public enum Size { RED("红色"), GREEN("绿色"), BLANK("白色"), YELLO("黄色"); //RED元素里面有几个变量,就要定义几个属性。如 RED("红色",1)则定义一个String和一个int private String color; //必须给所有的属性定义有参构造 private Size(String color) { this.color = color; } /* * 必须对属性set/get封装(安全) */ public String getColor() { return color; } public void setColor(String color) { this.color = color; } }
获取红色 : String redColor = Size.Size.RED.getColor();