读取文件中的常量#java [英] Read in file for constants #java
本文介绍了读取文件中的常量#java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想读取一个包含多个常量的数据文件(例如MAXARRAYSIZE)。
然后,我希望通过在程序中的任何位置键入以下常量来访问这些常量:ConstantsClassName.MAXARRAYSIZE。我该如何实现此类?
I want to read in a datafile that has several constants for my program (e.g. MAXARRAYSIZE).
I then want these constants to be accessible anywhere in my program by typing something like: ConstantsClassName.MAXARRAYSIZE. How do I implement this class?
从数据文件中分配这些常量之后,它们将永远不会在程序执行期间更改值。
Once assigned from the datafile, these constants will never again change value during program execution.
谢谢。
推荐答案
在 ConstantsClassName
类中使用静态块。
public class ConstantsClassName{
public static final String MAXARRAYSIZE;
static{
// read your file and store the data in;
MAXARRAYSIZE = valueRetrievedFromFile;
}
}
MAXARRAYSIZE $ c如果您遵循Java约定进行常量声明,则$ c>应该为
MAX_ARRAY_SIZE
。
这篇关于读取文件中的常量#java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文