如何在 Java 中创建一个“全局变量”,以便所有类都可以访问它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19939769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create a "global variable" in Java such that all classes can access it?
提问by Chrystle Soh
here's my problem: I have multiple classes that are part of the same package and they need access to a certain file path
这是我的问题:我有多个属于同一个包的类,它们需要访问某个文件路径
String filePath = "D:/Users/Mine/School/Java/CZ2002_Assignment/src/"
Rather than declaring the same Filepath in every single class, is it possible to simply have a "global" type of variable of this FilePath so that all classes can access it and I only need to declare and update it once.
与其在每个类中都声明相同的 Filepath,是否可以简单地拥有此 FilePath 的“全局”类型变量,以便所有类都可以访问它,而我只需要声明和更新它一次。
Thanks
谢谢
回答by Richard Tingle
If you declare it as
如果您将其声明为
public class TestClass {
public static String filePath="D:/Users/Mine/School/Java/CZ2002_Assignment/src/";
}
It will be accessible everywhere as TestClass.filePath
它将随处可见 TestClass.filePath
This can be useful (and your use case makes sense) but public static variables are a double edged sword and shouldn't be overused to just be able to access things which change from anywhere as they can break encapsulation and make your program less clear.
这可能很有用(并且您的用例是有道理的),但公共静态变量是一把双刃剑,不应过度使用以仅能够访问从任何地方更改的事物,因为它们可能会破坏封装并使您的程序不那么清晰。
If the string will never be changed for annother you can add the keyword final
, which will enforce this never changing behaviour as well as allowing the JVM to make additional efficiency enhancements (that you don't need to worry about)
如果字符串永远不会更改,您可以添加关键字final
,这将强制执行这种永不更改的行为,并允许 JVM 进行额外的效率增强(您无需担心)
回答by Blacklight
public class Test {
public static final String FILE_PATH = "D:/Users/Mine/School/Java/CZ2002_Assignment/src/";
}
Call it this way: Test.FILE_PATH
这样称呼它: Test.FILE_PATH
Note the final
because you only want to declare it once.
请注意,final
因为您只想声明一次。
There also used to be a code conventionto name final constants all uppercase, with components separated by underscore "_" characters. In the end, it's probably a matter of preference though.
还曾经有一个代码约定,将最终常量命名为全部大写,并用下划线“_”字符分隔组件。最后,这可能是一个偏好问题。
回答by Blacklight
public class One
{
public final static String FILEPATH = "D:/Users/Mine/School/Java/CZ2002_Assignment/src/";
}//class one
public class Two
{
public static void main(String[] args)
{
//sample operation to access the filePath value
System.out.println(One.FILEPATH);
}//main
}//class Two
Note:
笔记:
1) Ideally better to use a configuration file/properties file - that way you can change the path without recompilation.
2) Avoid using static variables! (almost always)
http://www.offthehill.org/articles/2011/06/03/java-static-variables-are-wrong-almost-always/
1)理想情况下最好使用配置文件/属性文件 - 这样您就可以更改路径而无需重新编译。
2)避免使用静态变量!(几乎总是)
http://www.offthehill.org/articles/2011/06/03/java-static-variables-are-wrong-almost-always/
回答by ZhongYu
A word on final
- if the string field is a constant variable, its value might be duplicated in many classes that reference it. We may want to avoid that because 1) the string is too big. 2) if the string is changed, we must recompile all classes that reference it.
一句话final
- 如果字符串字段是一个常量变量,它的值可能会在引用它的许多类中重复。我们可能希望避免这种情况,因为 1) 字符串太大。2) 如果字符串改变了,我们必须重新编译所有引用它的类。
We can avoid it by
我们可以通过
public static final String filePath;
static{ filePath="D:/Users/Mine/School/Java/CZ2002_Assignment/src/"; }
see http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.4
见http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.4
A variable of primitive type or type String, that is final and initialized with a compile-time constant expression (§15.28), is called a constant variable.
原始类型或字符串类型的变量,即最终的并使用编译时常量表达式(第 15.28 节)初始化的变量,称为常量变量。