Reflextions within static methods
I need to know the name of the class within a
static method.
In an instance method I can just use
this.getClass(), but not in a static method.
I need it because I have a class that inherits
from a singleton. But I can't use the inherited
static method getInstance(), it gives me
in instance of the superclass.
[362 byte] By [
Jakimo72a] at [2007-10-2 1:23:40]

you can get the current thread object using Thread.currentThread() and you can get the stackTrace trace of the current thread and find out the class and mathod name currently being executedbut the method Thread.getStackTrace is only available in java 1.5
LRMKa at 2007-7-15 18:45:21 >

Hello?Static methods are bound the the class they're coded in. It's not like a member method where the class could be a lot of different things.This is information known at compile time, and thus I can see no reason that you'd ever have to or want to do this dynamically
My idea was: I have an abstract singelton, I can inherit it from an other class
like this:
abstract public class Singelton {
static Singelton instance = null;
static Singelton getInstance(){
if(instance == null)
instance = new ...;//<-- Here is the gap I can't fill
return instance;
}
abstract public void doSomething();
}
public class Something extends Singelton {
public void doSomething()
{
System.out.println("Hello world!");
}
public static void main(String[] args)
{
Singelton s = Something.getInstance();
s.doSomething();
}
}
Can anybody help me to fill the gap in the code?
If I knew the class in the method getInstance(),
I could find out constructors name.
The idea will not work
> [code]
> abstract public class Singelton {
> static Singelton instance = null; <==== This will not get included into your subclass, since it is static
> static Singelton getInstance()<===== This won't get included either, again, since it is static
Static variables and methods are specified by the class and do not get overridden
I don't want to overwrite "instance" and "getInstance()",
I only want to use them in the context of the Class "Something".
Here is my next approach to the Idea:
abstract public class Singelton {
static Singelton instance = null;
static Singelton getInstance(Class thisClass){
if(instance == null)
{
try {
Constructor constructor = thisClass.getConstructor();
instance = (Singelton)constructor.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
}
return instance;
}
abstract public void doSomething();
}
public class Something extends Singelton {
public void doSomething()
{
System.out.println("Hello world!");
}
public static void main(String[] args)
{
Singelton s = Something.getInstance(Something.class);
s.doSomething();
}
}
The only little thing is, I would like to avoid
the parameter "thisClass" in the method "getInstance()",
because the method getInstance() in the class Singelton
is call thru the class Something.
Static methods were not made for inheritance. Thus, only Singleton's getInstance() method could ever be called, and it can't be over-ridden. I think the compiler should consider static methods final if it wants to enforce this, but that's another story entirely. In other words, what you're trying to do won't work - give up now before it's too late.
~Cheers
Trust me, I realy don't want to override any static methods, I only want to use them!Well my last approach works fine, the only thing is I don't like the parameter for the method getInstance().
The problem with your code is that
static Singelton instance = null;
will not get inherited by your subclasses. The static field will be available, but every class will see Singleton's field value they won't get their own.
You'll find that getInstance always returns an instance of the first class you tried to instantiate. For example, if you have two Singleton subclasses; class A and class B. See what happens:
Object a = A.getInstance(A.class); // Expected an instance of A
Object b = B.getInstance(B.class); // Expected an instance of B, but the instance of A is returned
System.out.println(a.getClass().getName());
System.out.println(b.getClass().getName());
The output will be
A
A
Also, you mispelled Singleton.