This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Static Vs Dynamic class loading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static Vs Dynamic class loading" Watch "Static Vs Dynamic class loading" New topic
Author

Static Vs Dynamic class loading

Bhairava Surya
Ranch Hand

Joined: Aug 19, 2007
Posts: 45
Hi,

Generally people call Static class loading when we create an instance using new keyword.
Dynamic class loading when we instance an object using class.forname().

But, generally all instances are created at run time only right?
then why the name static class loading?

and even in class.forname() also we are specifying the class name, in that why cant we create an instance using new keyword.

I have searched regarding this in our forum, but I am not satisfied with the answers, that why I am again raising this question.

Please reply me.

Thanks & Regards,
Ravi.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3605
    
  14

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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3605
    
  14

Yeah thanks, I wasn't exactly sure about it myself.
 
Don't get me started about those stupid light bulbs.
 
subject: Static Vs Dynamic class loading
 
Similar Threads
Static class loading and dynamic class loading
Static Initializers and Static, Dynamic class loading
Creating the instance of static nested class.
usage of Class.forName() for loading driver
Class.forName()