Solved

How to know I already load a class?

Posted on 2002-05-21
15
219 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet  URL Rewriting 1 37
Java exception bubble up 2 18
sql import cannot be resolved jsp 3 23
JAVA API design with micro service cloud in mind 1 18
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

809 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