aspose file tools*
The moose likes Java in General and the fly likes Loadin class VS Storing it in HashMap and taking it back for further use Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Loadin class VS Storing it in HashMap and taking it back for further use" Watch "Loadin class VS Storing it in HashMap and taking it back for further use" New topic
Author

Loadin class VS Storing it in HashMap and taking it back for further use

amod gole
Ranch Hand

Joined: Dec 07, 2005
Posts: 83
Hello All,

I have one question, I am in confusion , what is good if we compare loading class every time and using it for operation vs first time loading it and then store it in HashMap for further processing.

Thanks in advance
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

Can you explain in more detail (maybe with an example) what you mean exactly?

As far as I know, when you load a class in Java, it will never be unloaded (unless the class loader that loaded it is garbage collected itself). You don't need to manage loading classes yourself, the class loader does that for you. But I suspect that by "loading a class" you might mean something else than the Java class loading process?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

i think he means:





vs:


The HashMap seems to be an added dimension to the above approach

Is that your question amod ?

My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
amod gole
Ranch Hand

Joined: Dec 07, 2005
Posts: 83
Application is like this

one EJB which is calling different TaskHandler(interface implemented by different TaskHandler classes) for different Task. In Our application what we are doing we defined one HaspMap and in that we are storing these different TaskHandler so when new requests come to our EJB instead of loading TaskHandler we are checking availability of that taskHandler in HashMap if it is present then we are using that if not then we are loading it and storing it in same hashmap.

SO my question was what is best loading each time taskHandler means defining new object of task Handler or storing it in hashmap?. My EJB type is stateless and lets say 15 different taskHandler.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

Aha, so as I suspected, your question is not about class loading at all. So, to phrase it differently, your question is the following.

Your application has a TaskHandler interface and several classes that implement that interface, and now there's a HashMap that contains instances of those classes, and for each request the relevant TaskHandler is looked up in the HashMap and called. You're asking why that HashMap is being used, instead of just creating a new instance of the relevant TaskHandler for each request.

Probably the HashMap is meant as a cache, to avoid creating a lot of objects (instances of the classes that implement interface TaskHandler). If those classes are also stateless, it's not necessary to create multiple instances of those classes. Not creating a new object for each request would also make the application more scalable.
amod gole
Ranch Hand

Joined: Dec 07, 2005
Posts: 83
Thanks A Bunch........
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loadin class VS Storing it in HashMap and taking it back for further use