简体   繁体   English

在Java中声明项目常量的正确方法是什么?

[英]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中,所以要么:

  1. Pick a "main" class (say I have a project called "FTPServerApp" - I could put them there) 选择一个“主”类(假设我有一个名为“FTPServerApp”的项目 - 我可以把它们放在那里)
  2. Create a "Util" class that contains all of them 创建一个包含所有这些类的“Util”类

When you figure out where to put them, declare them all this way: 当你找出放置它们的位置时,请以这种方式声明它们:

public static final [type] [NAME_IN_ALL_CAPS] = [value];

This will 这将

  • make them available to all your project code, anywhere ( public ) 使它们可用于所有项目代码,任何地方( public
  • only one copy of the value exists across all instances of the class ( static ) 所有类的实例中只存在一个值的副本( static
  • they cannot be changed ( 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.

相关问题 在Java中的类中声明数组的正确方法是什么 - what is the proper way of declaring arrays in a class in Java 在Java中实现枚举来管理常量的正确方法是什么? - What is the proper way to implement enum to manage constants in Java? 部署具有JSON文件的Java项目的正确方法是什么? - What is the proper way to deploy Java project that has JSON files? 在Java的UI中显示资金的正确方法是什么? - What's the proper way to show money in an UI in Java? 在 Java 中存储应用程序的 conf 数据的正确方法是什么? - What is the proper way to store app's conf data in Java? 用Java编写自己的事件的正确方法是什么 - What's proper way to make my own events in java Java Swing-做基于阶段的GUI的正确方法是什么? - Java Swing - What's the proper way of doing a stage based GUI? 正确测试String属于常量子集的方式(Java) - Proper way testing that String belongs to subset of constants (Java) 在 Java Spring 引导项目中存储常用字符串(属性文件/常量类)的最佳方法是什么? - what is the best way to store common strings (properties file / constants class) in Java Spring boot Project? 用Java发布全局常量的更好方法是什么? - What is the better way of publishing global constants in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM