To make my question more clear, consider following use-case:
Suppose there is a package that permits a set of operations on a given platform, for example a class to edit the registry on Windows. This package does not exist on other platforms, as there is no equivalent operation on other operating system.
For the sake of simplicity, consider
windows/Registry.java
package windows;
public class Registry {
static Registry instance = null;
static{
System.out.println("print from static block");
}
private Registry() {
System.out.println("Registry instance created!");
}
public static synchronized Registry getInstance() {
if (null == instance) {
instance = new Registry();
}
return instance;
}
public void foo() {
System.out.println("foo called.");
}
}
and the class where I am going to conditionally use the registry: main/Main.java
package main;
import windows.Registry;
public class Main {
public static void test1(boolean onWindows) {
if (onWindows) {
Registry instance = Registry.getInstance();
System.out.println("We are on Windows: ");
instance.foo();
} else {
System.out.println("We are somewhere else!");
}
}
public static void main(String[] args) {
System.out.println("Entered main");
boolean onWindows = args.length > 0 ? Boolean.parseBoolean(args[0]) : false;
test1(onWindows);
}
}
The question is, if no function or class is explicitly executed in Main.class, is it guaranteed that no code from Registry.class is executed?
I was able to test this example on multiple Desktop Platforms and with different java versions, but I would like to know if this behavior is documented and one can rely on it, if it thus the expected behavior also on other platforms, like android or embedded version of the JRE.
In case there is no such guarantee, as a (maybe custom) classloader might decide to load all .class files in the classpath, can I assume that the code will work without a java.lang.NoClassDefFoundError if onWindows is false and I remove Registry.class from the classpath?
The behavior I've observed so far is
rm -rf bld; mkdir bld && javac -d bld src/windows/Registry.java src/main/Main.java && java -classpath bld main.Main true
Entered main
print from static block
Registry instance created!
We are on Windows:
foo called.
rm -rf bld; mkdir bld && javac -d bld src/windows/Registry.java src/main/Main.java && java -classpath bld main.Main false
Entered main
We are somewhere else!
rm -rf bld; mkdir bld && javac -d bld src/windows/Registry.java src/main/Main.java && rm -r bld/windows && java -classpath bld main.Main false
Entered main
We are somewhere else!
rm -rf bld; mkdir bld && javac -d bld src/windows/Registry.java src/main/Main.java && rm -r bld/windows && java -classpath bld main.Main true
Entered main
Exception in thread "main" java.lang.NoClassDefFoundError: windows/Registry
at main.Main.test1(Main.java:9)
at main.Main.main(Main.java:20)
Caused by: java.lang.ClassNotFoundException: windows.Registry
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
... 2 more
Are those behavior (lazy classloader and removing Registry.class) defined?