[英]What's the proper way of declaring project constants in Java?
This may seems a silly question for Java developers, however, I'm new to Java, and my background is from low level c. 对于Java开发人员来说,这似乎是一个愚蠢的问题,但是,我是Java的新手,我的背景来自低级别的c。 I used to include an header file with all the constants that were relevant for my projects. 我曾经包含一个头文件,其中包含与我的项目相关的所有常量。 (usually #define's). (通常是#define)。 I'm working on a big Java project now, and there a few constants I need to make global (they fit in more than one class, and used in various parts of the project ) 我现在正在开发一个大型Java项目,并且我需要创建一些全局的常量(它们适用于多个类,并在项目的各个部分中使用)
It makes it hard for me to decide where to put it, should I declare the same constant few times, one in each class ? 这让我很难决定把它放在哪里,我应该多次声明相同的常数,每个类一个?
A lot of framework, uses XML files to declare constants & definitions for the framework (Hibernate, Log4J, etc.) Is it wise to use this kind of technique in my project ? 很多框架,使用XML文件声明框架的常量和定义(Hibernate,Log4J等)在我的项目中使用这种技术是明智的吗? if so, how can it be done easily ? 如果是这样,怎么可以轻松完成?
As with many things, there are many ways to do it. 与许多事情一样,有很多方法可以做到。 One thing you should not do is declare them multiple times - that's just plain silly. 有一件事你不应该做的是多次宣称他们-那是太傻了。 :P :P
Everything has to be in a class in Java, so either: 一切都必须在Java中,所以要么:
When you figure out where to put them, declare them all this way: 当你找出放置它们的位置时,请以这种方式声明它们:
public static final [type] [NAME_IN_ALL_CAPS] = [value];
This will 这将
public
) 使它们可用于所有项目代码,任何地方( public
) static
) 所有类的实例中只存在一个值的副本( static
) final
). 他们无法改变( final
)。 The ALL_CAPS_FOR_CONSTANT_NAMES
, separated by underscores, is the convention in Java. 由下划线分隔的ALL_CAPS_FOR_CONSTANT_NAMES
是Java中的约定。
So, if this was declared in a class called FTPServerAPP
, and you had a constant called SERVICE_PORT
it might be: 所以,如果这是在一个名为FTPServerAPP
的类中声明的,并且你有一个名为SERVICE_PORT
的常量,它可能是:
public class FTPServerApp {
public static final int SERVICE_PORT = 21;
...
}
...and you would access it, from any class, like this... ......你可以从任何一个班级访问它,就像这样......
FTPServerApp.SERVICE_PORT
Take a look at enumeration types ( http://download.oracle.com/javase/tutorial/java/javaOO/enum.html ) They are supposed to provide a mechanism to supply constants without defining a concrete class (or an Interface with the desired constants, as is another option that people use). 看一下枚举类型( http://download.oracle.com/javase/tutorial/java/javaOO/enum.html )它们应该提供一种机制来提供常量而无需定义具体的类(或者一个接口与期望的常数,这是人们使用的另一种选择)。
One other technique I find helpful (similar to the FTPServerApp example given above) is to define a Context for whatever subsystem/component/etc... that holds not only the constants needed by components in that system, but can hold any state that you want to make more visible or don't want individual components to hold. 我发现另一种有用的技术(类似于上面给出的FTPServerApp示例)是为任何子系统/组件/等定义一个Context ...它不仅包含该系统中组件所需的常量,而且可以保存任何状态。想要使更多可见或不希望单个组件保持。 I believe this is along the lines of one of the GoF patterns, but it has been so long since I have looked at that book that I can't be certain (and I am too lazy to look it up right now!) 我相信这是GoF模式中的一个模式,但是我看了那本书后已经很久了,我无法确定(而且我现在懒得查看它!)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.