does an import statement really load the class?

hello, i was wondering if an import statement forces the class to be loaded, even when that class is never used?

example:


import java.awt.Point;

public class Dummy {

 public static void main(String[] args) {
  // do nothing
 }

}

will the java.awt.Point class be loaded even though it is never used?

thanks!

IIRC, no - import statements are simply a compile-time shortcut to prevent you having to type the fully qualified class name (eg. com.java.Foo ) every time you use something.

That’s correct, import statements do NOT load the classes.

I can compile an application, remove some classes from the JAR, ensure the methods requiring those classes are not invoked, and everything still runs. They are indeed a convenience for the programmer, and have no meaning after compilation.

A class isn’t even loaded before it is accessed at runtime, so in


public class MySingleton
{  
     // Wrapped singleton instance
     private static class InstanceHolder {  
         static MySingleton instance = new MySingleton();  
     }  
   
     // Lazy initialization, no synchronization  
     public static MySingleton getInstance() {  
         return InstanceHolder.instance;  
     }  
}

The singleton instance is created when calling “return InstanceHolder.instance;”, because the InstanceHolder class wasn’t loaded until then.

thank you all for your answers!