Jump to content
  • Advertisement
Sign in to follow this  
King of Men

[java] Dynamic class loading

This topic is 4662 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have two questions about this feature. First, could someone please explain to me what it is useful for? I have been Googling a bit, but I cannot seem to wrap my mind around why you would want to use it. If your example is in a games context, so much the better. Second, can one tell the compiler, or the JVM, to optimise by turning the feature off? It seems to me that the code for such a thing must occupy quite a bit of memory.

Share this post


Link to post
Share on other sites
Advertisement
Dynamic class loading is used everytime you start your app to load all the classes your app consist of. You can't turn it off.

Dynamic class loading can be used for dynamic code generation, you create bytecode and tell the JVM to load it. That way you can write highperformance code for special things you only get to know at runtime. Think game scripting language, you can compile your scripting stuff to bytecode and it runs at the same the speed as regular Java code.

Share this post


Link to post
Share on other sites
One example:

Your initfile (simple textfile so java.util.Properties can read it) contains key/value-pairs like this:

Breakout = mygames.arcade.breakout.BreakoutMain
Poker = mygames.card.poker.Poker
Bridge = mygames.card.bridge.Bridge

Key is the name displayed, and value is the name of the class to start the game with, i.e. is derived from general class 'Game'. Now you can easily add another game by an additional line in the initfile and putting the required class files into the classpath.

To load and start a game you can write:

Properties games = ...;
String name = games.getProperty("Poker");
Game game = (Game)(Class.forName(name).newInstance());
game.start(...);

Share this post


Link to post
Share on other sites
To give you a concrete example i once created a program wich analyzed java source code. You'd select directories containing java code and it would spit out some statistics about the file. Each statistic was actually a class implementing a "Statistic" interface. You had the total number of lines, average number of line per source, comment ratio,...
I made a plugin system so that you could add/remove statistic by adding/removing .class file in a plugin directory. All you had to do to create a new statistic is to create a class which implements the "Plugin" interface, generate the .class of that plugin and put it in the plugin folder. No need to change the code of the application to add a new plugin!
People could add their own statistic without possessing a single line of the cource code of my application (excepted the Plugin interface of course).

To do that, at startup my program would find all the .class file in the plugin directory, load those class dynamically and call methods dynamically to parse the source codes. It would have been impossible without dynamic loading.

Share this post


Link to post
Share on other sites
Sorry to resurect an old thread.

I am using dynamic class loading instead of scripting - they both solve the same problem. The advantage of dynamic class loading over scripting is that the class executes in the same environment and at the same speed as the rest of the java code.

Here's my code.



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!