Solved

How to know I already load a class?

Posted on 2002-05-21
15
217 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
  • 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues undeā€¦
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilationā€¦
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now