Java public/private 和 static 修饰符如何影响一行中声明的多个变量?

发布于 2024-08-30 10:07:28 字数 321 浏览 5 评论 0原文

以下内容等价吗?

private static boolean readAllFiles = false,readAllDirs = false;

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

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

private static boolean readAllFiles = false,readAllDirs = true;

Are the following equivalent?

private static boolean readAllFiles = false,readAllDirs = false;

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

And if so, do they still have the same modifiers with different values?

private static boolean readAllFiles = false,readAllDirs = true;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只想待在家 2024-09-06 10:07:28

是的,无论它们的初始值如何,它们都是等效的。

下面是一些测试代码来演示:

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);
  }
}

Yes, they are equivalent, regardless of their initial values.

Here is some test code to demonstrate:

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);
  }
}
国粹 2024-09-06 10:07:28

它们都是等价的。

您的最后一条语句:

private static boolean readAllFiles = false,readAllDirs = true;

相当于:

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

They are all equivalent.

Your last statement:

private static boolean readAllFiles = false,readAllDirs = true;

is equivalent to:

private static boolean readAllFiles = false;
private static boolean readAllDirs = true;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文