This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Vs Dynamic class loading

 
Bhairava Surya
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4840
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stephan van Hulst
Bartender
Pie
Posts: 4840
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah thanks, I wasn't exactly sure about it myself.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic