Is the Java class loaded if I access MyClass.class.getName ()?
I want to explicitly initialize some classes during initialization of my application with Class.forName
, but in order for this code to survive refactoring, I want to use this:
Class.forName(MyClass.class.getName());
I wonder if the class won't be loaded as soon as the method is executed getName
, making it unnecessary Class.forName
?
source to share
In fact, even a call is getName()
not needed, since the MyClass.class
class must be loaded and initialized for the object to exist .
Of course, this method means that you have a compile-time dependency on MyClass
that you do not have when used Class.forName()
with a string literal.
source to share