Reputation: 5668
Unsure about static variables.
import java.io.File;
public class Logger {
public static final File log = new File(File.listRoots()[0], "log.log");
public static void log (String message) {
/* ... */
}
}
Is the variable log
pointing to the same memory throughout the life of the program? Basically is the log
definition new File(File.listRoots()[0], "log.log")
calculated multiple times or just one, and when?
Thanks in advance.
Upvotes: 5
Views: 1132
Reputation: 597016
It is invoked once per classloader. Which, normally, means once.
A static variable is initialized as soon as the class declaring it is loaded by the classloader, and stays there until the classloader is destroyed, which in most cases means - at the end of the program execution / application lifecycle.
Upvotes: 11