0

I have a class which is supposed to supply paths to the program. In order to do so, it determines the local and the roaming data path under Windows and just takes a sub directory of home under Linux. Besides that, it provides paths derived from the other ones.

Is it safe to write

public class DataDirectory {
    public static final File ROAMING;
    public static final File LOCAL;

    static {
        // set ROAMING and LOCAL
    }

    public static final File PROFILE =  doMkdirs(new File(ROAMING, "profiles");
    public static final File SETUP =  doMkdirs(new File(ROAMING, "setup");
    public static final File LOGFILES =  doMkdirs(new File(ROAMING, "logfiles");

    private static File doMkdirs(File file) {
        file.mkdirs();
        return file;
    }
}

or do I have to do

public class DataDirectory {
    public static final File ROAMING;
    public static final File LOCAL;

    public static final File PROFILE;
    public static final File SETUP;
    public static final File LOGFILES;

    static {
        // set ROAMING and LOCAL

        PROFILE = mkdirsRoaming("profiles");
        SETUP = mkdirsRoaming("setup");
        LOGFILES = mkdirsRoaming("logfiles");
    }

    public static final File PROFILE = doMkdirs(new File(ROAMING, "profiles");
    public static final File SETUP = doMkdirs(new File(ROAMING, "setup");
    public static final File LOGFILES = doMkdirs(new File(ROAMING, "logfiles");

    private static File doMkdirs(File file) {
        file.mkdirs();
        return file;
    }
}

on order to be safe?

glglgl
  • 89,107
  • 13
  • 149
  • 217

1 Answers1

0

I answer this question by myself in QA style.

The java specs state that the initializers are executed in textual order, as though they were a single block.

That means that the first version is safe; the initializers after the static { } block can make use of the final variables set up inside that block.

glglgl
  • 89,107
  • 13
  • 149
  • 217