java java中extern的等效关键字是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6830637/
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
What is the equivalent keyword for extern in java?
提问by Thangaraj
I have one public static variable in one file and how can I export the same variable to other files?
我在一个文件中有一个公共静态变量,如何将相同的变量导出到其他文件?
For ex:-
例如:-
file1.java
文件1.java
public final static int buf = 256;
file2.java
文件2.java
How can I access the variable "buf" in this file?
如何访问此文件中的变量“buf”?
回答by Bozho
File1.buf
. More generally: ClassName.FIELD_NAME
. A few notes
File1.buf
. 更一般地说:ClassName.FIELD_NAME
。一些注意事项
- use
CAPITAL_LETTERS
to name your static final fields (BUF
) - use
static final
rather thanfinal static
(not crucial, but it is a widely accepted style)
- 用于
CAPITAL_LETTERS
命名您的静态最终字段 (BUF
) - 使用
static final
而不是final static
(不重要,但它是一种被广泛接受的风格)
回答by Saher Ahwal
File1.buf
correct definition: public static final int BUF = 256;
正确定义: public static final int BUF = 256;
so you would call it in other class:
所以你会在其他类中调用它:
File1.BUF;