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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:28:07  来源:igfitidea点击:

What is the equivalent keyword for extern in java?

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_LETTERSto name your static final fields (BUF)
  • use static finalrather than final 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;