This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
You are confusing class instantiation with class loading. Before the class can be instantiated, the JVM must "know what it is". To do this, it loads the class. Loading takes place when the JVM starts up. I think it's more complex than that, but for now you can think of all your classes being loaded before your program starts.
You can load a class later though, during runtime, with the forname method. This is called dynamic because you do it while your program is already running.
I know precious little about this subject, but I believe forname makes it possible for you to make a program that creates a new class file and then load it and use it. You can't do this with the new keyword, because the class must already be present.
The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Stephan van Hulst wrote:Loading takes place when the JVM starts up. I think it's more complex than that, but for now you can think of all your classes being loaded before your program starts.
Let's take away that delusion, shall we?
Even with static loading, the class is loaded once it's needed. That can be when you call a static method, access a static field, or create an instance, but it won't be loaded if it's not needed.
I think the static vs dynamic is related to the required class availability. Staticly loaded classes must be available during compile time and runtime, whereas dynamicly loaded classes only need to be available at runtime. That could mean creating a class on the fly during runtime (using the right tools), or downloading the JAR file with the class file in it during runtime and using a different ClassLoader to load it.