Object pooling

I have an abstract class, and I want to put object pooling code into this class. something like:

public abstract class A {
   static public A getInstance() {...}

how can getInstance 'know' the class it is running in? as in can getInstance get at the Class object it is running in? like...

public class B extends A {

so how can B.getInstance know that it is running 'within' B?

If there is any other way of providing object pooling code from a superclass aside from using instance methods that's welcome too.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Would it help to get the classes name?

You can do that with:

Class c=getClass();
String n=c.getName();
_lychee_Author Commented:
doesn't getClass only work from instance methods?

i know how to do it from instance methods... but i'm wondering if it's possible for static methods to do this
Never the easy questions, lychee....



As you also cannot do static inheritance I think the answer is no.

This sort of object management needs an instance. You could do it from a factory but that would

Write your own class loader for this? EG:

For example, an application could create a network class loader to download class files from a server. Sample code might look like:

   ClassLoader loader = new NetworkClassLoader(host, port);
   Object main = loader.loadClass("Main", true).newInstance();
        . . .
The network class loader subclass must define the methods findClass and loadClassData to load a class from the network. Once it has downloaded the bytes that make up the class, it should use the method defineClass to create a class instance. A sample implementation is:

     class NetworkClassLoader extends ClassLoader {
         String host;
         int port;

         public Class findClass(String name) {
             byte[] b = loadClassData(name);
             return defineClass(name, b, 0, b.length);

         private byte[] loadClassData(String name) {
             // load the class data from the connection
              . . .
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Just thought of a possible solution but haven't tried it out.

If you have a static code block in the abstract base class that has a try/catch block and in that try catch block deliberately throws an exception does the stack trace tell you the correct class name?

If so, you could capture the stack trace printed to system.err and parse it for the class name of this class in the catch block and then have code in the catch or finally blocks to use this class name as you wish.

// static code block

  try {
    //forcibly throw an exception
  } catch (Exception e) {
    // parse exception to get class name


Slightly messy, but this may well work as the JVM obviously knows the class name - you just need to force it to tell you somehow.

(heyhey has posted some code previously for parsing a stack trace I believe...)
Ahh nope - hung by my own petard there as the Static block will only run once and there is no way you can force subclasses to initialise it again.

What you need is a tool to get inside the JVM and retrieve the class name. Possibly a llittle excessive for your purposes.

What about using an interface and having different static initialisation in your subclasses that way?

If you have not seen,



He has done connection poolong. I thimnk same is applicable to object pooling.
_lychee_Author Commented:
jod... tried ur exception method but unfortunately it reports it as from the originating class... tis a pity that there's no static inheritance i guess :(

i guess i'll have to put the object pooling code in the factory class (in line with my other qn as well i guess)... but then returning the object to the pool gets troublesome (cos factory has to identify the correct pool/alternatively, factory has to search the pool when giving out the object)......

*sigh* i guess i'll just have to use the instance thingy...

ravindra, i can't connect to the url u gave... maybe my school's network slightly bozo now...

jod, i think i'll reduce the points for this and the other qn by 1/2 (so total = 200), and then accept ur comment; is that ok with u?
>> so how can B.getInstance know that it is running 'within' B?

in fact B.getInstance is not running 'within' B. there is only one definition of getInstance method, which can be called some other method (static or instance) 'within' class B.

1. you can't override static methods.
2. you can't get the Class object you are 'within' (from a static method).

but ... cheating :)
1. you can get (almost always)
- the class name
- the method name
- the java file name
- line number
for any piece of code. (static / instance method)

I have used successfully this technique for logging purposes.
I can post some (ugly :) example code, but generally speaking you
throw and Exception and parse its stacktrace (Jod mentioned it ?).

you can implement your A.registerInstance and A.getInstance

A.registerInstance(A object)
  // find the caller class name (class B)
  // instances.put(callerName, object)

  // find the caller class name (class B)
  // return (A)instances.get(callerName)

3. you'd better change your design :)
since Java classes are not 'real objects' (hmmm ... I have to check
what's the result from obj.getClass().getClass())  you'd better leave
all the static stuff and make your own Factory hierarchy - you can
have Factories that makes Factory objects and you'll be able to override
Factory methods ...)
_lychee_Author Commented:
pt well taken :)
so -
1. do you want my sample (ugly :) code ?
2. who will take the points ? :)
_lychee_Author Commented:
1. 'tis not ethical to 'cheat' don't u think :)
2. jod i guess :>
1. not ethical, but you have to sometimes ... cheat or die :)
2. it's ok :) you can accept his comment as answer
1. we prefer to say over here "It's OK to cheat as long as you don't get caught"

2. you have been :)

lychee feel free to reduce the points down to a more reasonable level for these two quesstions as you suggest.

tis not ethical to be greedy...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
_lychee_Author Commented:
alright then...
Community Support has reduced points to 100
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.