I want to have the same static variable with a different value depending on the type of class.
So I would have
public class Entity
{
public stat
I had the same question and came to the solution to use a static mapping
Class --> Object.
The following code example uses Integer as the type of the desired "class-static" variable.
import java.util.Map;
import java.util.HashMap;
class C
{
static Map, Integer> class2IntegerMap = new HashMap, Integer>();
public void setClassSpecificInteger(Integer _i)
{
class2IntegerMap.put(this.getClass(), _i);
}
public Integer getClassSpecificInteger()
{
return class2IntegerMap.get(this.getClass());
}
}
class CA extends C
{
}
class CB extends C
{
}
class CAA extends CA
{
}
public class MainClass
{
public static void main(String []args)
{
CA a1 = new CA();
CA a2 = new CA();
CB b1 = new CB();
CB b2 = new CB();
CAA aa1 = new CAA();
a1.setClassSpecificInteger(Integer.valueOf(-1));
b1.setClassSpecificInteger(Integer.valueOf(+33));
System.out.println("The int-value for a1 is: "+a1.getClassSpecificInteger());
System.out.println("The int-value for b1 is: "+b1.getClassSpecificInteger());
System.out.println("The int-value for aa1 is: "+aa1.getClassSpecificInteger());
System.out.println("The int-value for a2 is: "+a2.getClassSpecificInteger());
System.out.println("The int-value for b2 is: "+b2.getClassSpecificInteger());
CA a3 = new CA();
CB b3 = new CB();
System.out.println("The int-value for a3 is: "+a3.getClassSpecificInteger());
System.out.println("The int-value for b3 is: "+b3.getClassSpecificInteger());
CAA aa2 = new CAA();
aa2.setClassSpecificInteger(Integer.valueOf(8));
System.out.println("The int-value for aa1 now is: "+aa1.getClassSpecificInteger());
}
}
The output is:
The int-value for a1 is: -1
The int-value for b1 is: 33
The int-value for aa1 is: null
The int-value for a2 is: -1
The int-value for b2 is: 33
The int-value for a3 is: -1
The int-value for b3 is: 33
The int-value for aa1 now is: 8
I hope this helps someone. Please be kind.