How do public / private and static Java modifiers affect multiple variables declared on the same line?

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;
+3
source share
2 answers

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

Here is a sample test code:

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);
  }
}
+9
source

All of them are equivalent.

Your last statement:

private static boolean readAllFiles = false,readAllDirs = true;

is equivalent to:

private static boolean readAllFiles = false;
private static boolean readAllDirs = true;
+3
source

Source: https://habr.com/ru/post/1743410/


All Articles