It's not a secret anymore!*
The moose likes Java in General and the fly likes ClassLoaders Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ClassLoaders" Watch "ClassLoaders" New topic
Author

ClassLoaders

Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 248
Hi All,

Please could anyone give me an example and explain how to write our own ClassLoaders

Thanks and Regards,
Siddharth
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

Some good articles about ClassLoaders

RanchFAQ


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 248
I want to write ClassLoader for my Singleton class. Please help me with code example.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

Why would you want to do that? A ClassLoader is for loading classes, not instances.

I take it you want to properly serialize / deserialize, while still keeping the singleton property intact, am I right?

If so, you can use the readResolve method:

This will cause any Singleton instance you deserialize to be the same (==) as INSTANCE.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 248
Hi Rob,
I know about serializing and de serializing an singleton instance. I was asking about the situation how we can write a classloader for a singleton class so that classes loaded by different classloaders don't create more than on singleton instances.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
Questions on theory
If an app can use more than one class loader, how do the classloaders know that other CL are being used and so be able communicate to control/restrict what is loaded?
To know that a class is a Singleton implement an interface that states the fact. Like Cloneable.
Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 248
Please help me in writing my ClassLoader for a Singleton class and please also tell me how could I test it to make sure that it only loads one instance of my class.

Please help in clearing my concepts.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39551
    
  27
Why do you need a ClassLoader to use a Singleton?


Ping & DNS - updated with new look and Ping home screen widget
Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 248
I have read that "If the Singleton class is loaded by 2 different class loaders we'll have 2 different classes, one for each class loader."
I want to make sure that Singleton Design Pattern principle is not violated. So I want to write my own class loader for my Singleton class so that it doesn't violate the Singleton Design Pattern principle.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39551
    
  27
Well, the more classloaders you add, the higher the chances that more than one of them will load the class. What kind of application are you talking about? E.g., desktop applications generally have only a single classloader for user code, so unless the application creates more classloaders (which would be unusual), there shouldn't be a problem.

In servlet containers, on the other hand, there are multiple classloaders, and the only way to make sure that within the JVM there is only a single instance of a class is to use it in just one web app, or to have it loaded by the servlet container itself and by none of the web apps.

You can't solve a problem arising form the existence of several classloaders by writing one of your own.
Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 248
Well, I am just trying to make a sample desktop application in Eclipse through which I am trying to prove/learn for myself and clear my concept that if we write our own class loader then a class would have only a single instance. I read somewhere that for ensuring that a class to be truly Singleton we have to write our own class loader to load our Singleton class. Is it true if not then please so clear my concept.


What kind of application are you talking about?


Is this applicable for desktop applications or for web applications ??? And what is the solution which is prescribed for this.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39551
    
  27
I am just trying to make a sample desktop application in Eclipse through which I am trying to prove/learn for myself and clear my concept that if we write our own class loader then a class would have only a single instance.

No. Each class will have exactly one instance if that's how often an object of that class gets instantiated. As was said before, class loaders load classes, not objects. They're not a solution to this problem (to the contrary, they can make it significantly harder).

I read somewhere that for ensuring that a class to be truly Singleton we have to write our own class loader to load our Singleton class. Is it true if not then please so clear my concept.

It doesn't matter how many classloaders you have, or if you have written them yourself. If you want to make sure that there's only a single instance of a class, then you must ensure that it gets instantiated no more than a single time, and that only a single classloader does that.

Is this applicable for desktop applications or for web applications ?

This is true in general. But servlet containers use more classloaders than desktop applications, so you have more opportunities to instantiate a class more than once, if you're not careful about which classloader has access to the class.

And what is the solution which is prescribed for this.

I'd start by learning a lot more about classloading. This is a good introduction. Once you know the differences between the boot classloader, the extensions classloader and the system classloader you can start to think about ways how to avoid loading a class more than once.
[ September 02, 2008: Message edited by: Ulf Dittmer ]
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Originally posted by Siddharth Bhargava:
Well, I am just trying to make a sample desktop application in Eclipse through which I am trying to prove/learn for myself and clear my concept that if we write our own class loader then a class would have only a single instance.


Well, if you are using the Eclipse Framework, then you are using OSGi and if you are using OSGi, you should NOT expose a singleton class that you need to have perfect controll over OUTSIDE your bundle. Instead you should create a service that hands out the one and only instance of your singleton class to whom it may concern.
 
wood burning stoves
 
subject: ClassLoaders
 
Similar Threads
Clasloaders in jboss
Help!!! I can't acces my class from JSP without a package
ClassLoaders...
classloaders in a jvm
Invalid method Code length