79386559

Date: 2025-01-25 10:49:30
Score: 1
Natty:
Report link

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!
Reasons:
  • Blacklisted phrase (0.5): how can I
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: Andios