博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在Java中定义常量(Constant)
阅读量:5966 次
发布时间:2019-06-19

本文共 2789 字,大约阅读时间需要 9 分钟。

原本引自  

1 /** 2  * Method One 3  */ 4 interface ConstantInterface { 5     String SUNDAY = "SUNDAY"; 6     String MONDAY = "MONDAY"; 7     String TUESDAY = "TUESDAY"; 8     String WEDNESDAY = "WEDNESDAY"; 9     String THURSDAY = "THURSDAY";10     String FRIDAY = "FRIDAY";11     String SATURDAY = "SATURDAY";12 }13 /**14  * Method Two 15  */16 enum ConstantEnum {17     SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY18 }19 /**20  * Method Three21  */22 class ConstantClassField {23     public static final String SUNDAY = "SUNDAY";24     public static final String MONDAY = "MONDAY";25     public static final String TUESDAY = "TUESDAY";26     public static final String WEDNESDAY = "WEDNESDAY";27     public static final String THURSDAY = "THURSDAY";28     public static final String FRIDAY = "FRIDAY";29     public static final String SATURDAY = "SATURDAY";30 }31 /**32  * Method Four33  * http://www.ibm.com/developerworks/cn/Java/l-java-interface/index.html34  */35 class ConstantClassFunction {36     private static final String SUNDAY = "SUNDAY";37     private static final String MONDAY = "MONDAY";38     private static final String TUESDAY = "TUESDAY";39     private static final String WEDNESDAY = "WEDNESDAY";40     private static final String THURSDAY = "THURSDAY";41     private static final String FRIDAY = "FRIDAY";42     private static final String SATURDAY = "SATURDAY";43     public static String getSunday() {44         return SUNDAY;45     }46     public static String getMonday() {47         return MONDAY;48     }49     public static String getTuesday() {50         return TUESDAY;51     }52     public static String getWednesday() {53         return WEDNESDAY;54     }55     public static String getThursday() {56         return THURSDAY;57     }58     public static String getFirday() {59         return FRIDAY;60     }61     public static String getSaturday() {62         return SATURDAY;63     }64 }65 public class TestConstant {66     static final String day = "saturday";67     public static void main(String[] args) {68         System.out.println("Is today Saturday?");69         System.out.println(day.equalsIgnoreCase(ConstantInterface.SATURDAY));70         System.out.println(day.equalsIgnoreCase(ConstantEnum.SATURDAY.name()));71         System.out.println(day.equalsIgnoreCase(ConstantClassField.SATURDAY));72         System.out.println(day.equalsIgnoreCase(ConstantClassFunction73                 .getSaturday()));74     }75 }

 

方法一采用接口(Interface)的中变量默认为static final的特性

方法二采用了Java 5.0中引入的Enum类型。

方法三采用了在普通类中使用static final修饰变量的方法。

方法四类似方法三,但是通过函数来获取常量。

 

首先定义全局变量似乎有违Java的面向对象的封装特性,增加的耦合。所以最佳的方法是避免定义全局变量。如果是参数等,可以写入配置文件。如果实在是必须的,方法二是最为推荐的。方法三是大家都能想到的,非常的直观。方法一和方法三本质上一样。方法四提供了灵活性,具体参考引用【1】。

参考

 

转载于:https://www.cnblogs.com/DreamDrive/p/5413255.html

你可能感兴趣的文章
swift基础之_swift调用OC/OC调用swift
查看>>
Devexpress 15.1.8 Breaking Changes
查看>>
Java B2B2C多用户商城 springcloud架构- common-service 项目构建过程(七)
查看>>
ElasticSearch Client详解
查看>>
新零售讲堂之时代下的传统零售业,何去何从?
查看>>
c++读取和写入TXT文件的整理
查看>>
linux安全问答(1)
查看>>
mybatis update返回值的意义
查看>>
expdp 详解及实例
查看>>
解读最具O2O属性—哈根达斯微信企业号的成功之道
查看>>
Extjs4.x (MVC)Controller中refs以及Ext.ComponentQuery解析
查看>>
Server-01 How to Find the Remote Desktop Port
查看>>
Java--接口、抽象与继承
查看>>
通过IP判断登录地址
查看>>
Oracle闪回技术
查看>>
利用单壁路由实现vlan间路由
查看>>
hello world
查看>>
CentOS 7 配置yum本地base源和阿里云epel源
查看>>
python 学习导图
查看>>
生成树
查看>>