Solved

How to know I already load a class?

Posted on 2002-05-21
15
221 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jdbc error in jsp application 20 88
running on tomcat not jboss eap 7.0 3 52
What is the use of Forwarding Class in java 1 52
netbeans can not run self signed app 8 37
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

734 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