Solved

How to know I already load a class?

Posted on 2002-05-21
15
222 Views
Last Modified: 2012-05-04
I have one application that will call up many class. But every class is only allow to be start once.

How to detect whether a class is running?

Guys, any idea?
0
Comment
Question by:realman1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +3
15 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 7023559
 You mean load it only once? What you can do is to maintain a hashtable of classes and when asked return the *exact* class object reference. You will have to check the Hashtable every time a class is requested in order to see if it is already there or not. If it is return it, otherwise load it and put it in the hashtable.

  The following might come in handy: http://www.javaworld.com/javaworld/jw-10-1996/jw-10-indepth.html

  Hope it helps.
0
 
LVL 3

Expert Comment

by:shyamkumarreddy
ID: 7023560
Realman.

Pls refer to the documentation for ClassLoader and it has method called findLoadedClass() which returns class or null if it is not loaded in the memory.

Thanks
Shyam
0
 
LVL 1

Expert Comment

by:BaneBane
ID: 7023575
Why don't you use the singletone design pattern. this pattern alows for only one instance of a class to be loaded.

I'v included a sample code.

class x{

   private X m_Instance = null;

   /**
    private constructor, thus it can not be called.
   */  
   private x(){
   }
   
   public synchronized X getInstance(){
     if (m_Instance==null)
        m_Instance = new x();

     return m_Instance;
   }
}


Hope this helps.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 3

Expert Comment

by:shyamkumarreddy
ID: 7023582
bane
Pls don't lock the question. Just put ur info on comments still comments can be choosen as solution

Cheers
SHyam
0
 
LVL 1

Expert Comment

by:BaneBane
ID: 7023585
opps sorry, I'm new to this
0
 
LVL 35

Expert Comment

by:girionis
ID: 7023586
 BaneBane please do not propose answers, only comments instead, as this locks the question and it is difficult for other people to give an answer.

  Thank you.
0
 
LVL 35

Expert Comment

by:girionis
ID: 7023588
 OOps sorry, I did not see shyamkumarreddy's comment.
0
 
LVL 3

Expert Comment

by:shyamkumarreddy
ID: 7023589
Thanks for taking advice Bane. Even i did this mistake when i am new to this society. Always keep learning things :)

Shyam
0
 
LVL 1

Expert Comment

by:BaneBane
ID: 7023590
again sorry
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7023736
... or if you create these classes, you can use static initialization:

class MyClass() {
  private static boolean started = false;
 
  ...
 
  public void start() {
    if (started) return;
   
    started = true;
    ...
  }
}
0
 
LVL 9

Accepted Solution

by:
Ovi earned 50 total points
ID: 7051053
1. To allow only one instance at a time, the right solution is the singleton pattern. Here is a better implementation :

public final class A {
  private static A a;

  private A() {
  }
 
  public static final A getA() {
    if(a == null)
      a = new A();
    return(a)
  }
}

I've introduced the final modifier to the class declaration to discourage inheritance and so pattern exposure. Also the example provides lazzy initialization which means that the object a is created only first time when is used (avoid memory allocation problems).

2. If you want to see which classes are instantiated at a given moment in time, I believe that is hard to do and java does not give you (as far as I know) a way to see that. A class loaded via classloader is not the same thing with an instance of the respective class. One solution to solve this is to implement a special class thru which you can create your instances. This class will also maintain a hashtable with the instantiated classes.

Something like :

public class ObjectCreator {
  private static final Hashtable instances = new Hashtable();
  pubic static final Object createInstance(String className) {
    try {
      Class c = Class.forName(className);
      Object inst = c.newInstance();
      instances.put(className, inst)
      return(inst);
    } catch(Exception e) {
      return(null);
    }
  }

  public static final Object getInstance(String className) {
    return(instances.get(className));
  }

.....................................
}

Note: the above code works for allready known classes thru classpath variable. If you want to load also classes which are not contained in the classpath, you should do this via a custom classloader.
0
 
LVL 9

Expert Comment

by:Ovi
ID: 7051062
...optimization :

  public static final Object getInstance(String className) {
    Object o = instances.get(className);
    return((o != null)?o:createInstance(className));
  }

0
 

Author Comment

by:realman1
ID: 7052503
Ovi help will be more suitable for my application.
Anyway, thank you guys. :)
0
 

Author Comment

by:realman1
ID: 7052509
I will go for second choice. :)
0
 

Author Comment

by:realman1
ID: 7052529
I will go for the second one. :)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question