I found this topic while searching how can I know "If a class is loaded?" I was reading a question on static initializer. Basically the book says the static initializer is executed if the class is loaded; And it may never be executed if the class is not loaded. Out of curiosity I wrote this simple class. The class containing the main() is always loaded if the program is executed. Class C was referenced and it's loaded. Class B was not referenced and not loaded.
class B
{
static
{
System.out.println("Class B is loaded!");
}
}
class C
{
static
{
System.out.println("Class C is loaded!");
}
}
public class A
{
static
{
System.out.println("Class A is loaded!");
}
public static void main (String [] args) {
C c = new C();
}
}
and the executing results are:
Class A is loaded!
Class C is loaded!