• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

ClassLoaders

 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

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

Thanks and Regards,
Siddharth
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some good articles about ClassLoaders

RanchFAQ
 
Siddharth Bhargava
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to write ClassLoader for my Singleton class. Please help me with code example.
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Siddharth Bhargava
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 5035
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you need a ClassLoader to use a Singleton?
 
Siddharth Bhargava
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
I'm sure glad that he's gone. Now I can read this tiny ad in peace!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic