Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to know I already load a class?

Posted on 2002-05-21
15
Medium Priority
?
224 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

618 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