编程语言
首页 > 编程语言> > Java公共/私有和静态修饰符如何影响在一行上声明的多个变量?

Java公共/私有和静态修饰符如何影响在一行上声明的多个变量?

作者:互联网

以下是等效的吗?

private static boolean readAllFiles = false,readAllDirs = false;

private static boolean readAllFiles = false;
private static boolean readAllDirs = false;

如果是这样,他们是否仍然拥有不同值的相同修饰符?

private static boolean readAllFiles = false,readAllDirs = true;

解决方法:

是的,它们是等价的,无论它们的初始值如何.

这是一些测试代码来演示:

public class TestClass
{
  private static boolean readAllFiles = false,readAllDirs = true;

  public static void main(String[] args)
  {
    //these two would result in COMPILE error if both vars were not static
    System.out.println("readAllFiles: " + readAllFiles);
    System.out.println("readAllDirs: " + readAllDirs);
  }
}
public final class TestClass2
{
  public static void main(String[] args)
  {
    //these two DO result in COMPILE error, because both vars are private
    System.out.println("TestClass.readAllFiles: " + TestClass.readAllFiles);
    System.out.println("TestClass.readAllDirs: " + TestClass.readAllDirs);
  }
}

标签:java,syntax,access-modifiers,static,declaration
来源: https://codeday.me/bug/20190715/1472583.html