• Post Reply Bookmark Topic Watch Topic
  • New Topic

What does class loader means?  RSS feed

 
Jack Lau
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

Could anyone tell me what does class loader means? When do we use it? Why do not simpily use new keywords to instantiate an object from a class?

Thanks,
Jack
 
Scott Dunbar
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A classloader loads classes from appropriate places. In a J2EE server, for example, there are at least three class loaders. The first is the System class loader. This is for classes that are on the system classpath (that is, things specified via -classpath or on the CLASSPATH environment variable). The second is for enterprise application specific classes. The third would be for web applications. Classes that are found, for example, on the System classpath are visible to the lower classloaders, but not the other way around.

The point of this is to isolate things like web applications into their own environments. Two web applications in a server may have different versions of the same jar file. Since each web app effectively has a classloader all to itself that is possible. If everything had to be on the System classpath then there could only be one version of the jar.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use new a vast majority of the time. It compiles into something that uses the ClassLoader on your behalf but you don't actually have to see it.

There are some neat places to use Class.forName("string").newInstance(); Again, this uses the ClassLoader even though you don't see it. I use this in places where I get the actual class name from configuration at runtime and don't know it at compile time. This is slightly advanced ... if it sounds interesting lemme know and we can follow up.

Then there are some tricky places where you have to know about ClassLoader itself. Some environments like EJB containers use a hierarchy of class loaders, each of which knows about more or different classpaths. When you say "new" you use the default class loader which may not know about the class you're trying to build. I've had to get the classloader that loaded the object my code is in and use it to load other classes. It sounds convoluted, but the rules are really pretty clear. Again, ask if you want more on that.

Hope that helped. Keep asking questions!
 
Cay Horstmann
author
Ranch Hand
Posts: 197
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, application servers use class loaders a lot. But I think the applet class loader is a bit easier to understand.

When a browser loads an applet, the applet is executed by a Java VM. The VM needs to fetch the bytes for the classes from the internet--they aren't on the same machine yet. (You may wonder why the browser doesn't just fetch the class files, but a browser wouldn't know that some classes need other class files to function.) This is done by a class loader.

Now think of two applets, written by two different people, that accidentally have the same class name, e.g. MyCoolApplet. The VM uses two DIFFERENT class loaders to load the two applets. Each class loader loads "its" MyCoolApplet.class file, and the VM can execute both applets at the same time. You can have multiple classes with the same exact name, provided they are loaded by different class loaders.

You can't imitate this behavior with a simple call to "new".

Of course, this is a specialized situation that only happens in a progam (such as a browser or an app server) that needs to load arbitrary classes from arbitrary sources.

HTH,

Cay
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!