Java and inherited static members [duplicate]

2019-01-18 11:01发布

This question already has an answer here:

Suppose i have the below class:

class Parent
{
    private int ID;
    private static int curID = 0;

    Parent()
    {
         ID = curID;
         curID++;
    }
}

and these two subclasses:

class Sub1 extends Parent
{
    //...
}

and

class Sub2 extends Parent
{
    //...
}

My problem is that these two subclasses are sharing the same static curID member from parent class, instead of having different ones.

So if i do this:

{
    Sub1 r1 = new Sub1(), r2 = new Sub1(), r3 = new Sub1();
    Sub2 t1 = new Sub2(), t2 = new Sub2(), t3 = new Sub2();
}

ID's of r1,r2,r3 will be 0,1,2 and of t1,t2,t3 will be 3,4,5. Instead of these i want t1,t2,t3 to have the values 0,1,2, ie use another copy of curID static variable.

Is this possible? And how?

9条回答
成全新的幸福
2楼-- · 2019-01-18 11:05

static members are part of the Parent.class object in the PermGen part of the JVM. All instances of that class share the same static variables.

Every subclass should have its own static curID.

查看更多
\"骚年 ilove
3楼-- · 2019-01-18 11:10

Unlike what other answers said, there IS a solution to your problem, though it doesn't involve "static inheritance". You should have a per-class ID generator.

Here is a good example:

Java: Parent Methods accessing Subclasses' static variables?

查看更多
可以哭但决不认输i
4楼-- · 2019-01-18 11:11

As others already wrote, static members are bound to the class, so you need to track the id on a class level, e.g. like this:

abstract class Parent {
    private int ID;

    Parent() {
         ID = nextId();
    }

    abstract protected int nextId();
}

class Sub1 extends Parent {
    private static int curID = 0;

    protected int nextId() {
       return curID++;
    }

    //...
}

class Sub2 extends Parent {
    private static int curID = 0;

    protected int nextId() {
       return curID++;
    }

    //...
}

Note that this approach is not thread safe - but neither was the code in the question. You must not create new objects from the same sub class concurrently from different threads.

查看更多
狗以群分
5楼-- · 2019-01-18 11:13

Statics are statics are statics. There is only a single instance of curID, period. So if you want separate counters for Sub1 and Sub2, you declare the static in each of those classes.

查看更多
趁早两清
6楼-- · 2019-01-18 11:21

Static elements are not inherited at all.

There is not actually any such thing as Sub1.curID - this is a legal (yet confusing) way of referring to Persion.curID.

Unfortunately there isn't any way to do what you're after, with static references. They fundamentally do not and cannot work with inherentance - since they're statically resolved, they can't rely on dynamic dispatch and so cannot depend on runtime polymorphism. Given that the JVM/compiler treats static variables this way, I'm confident that there's no workaround you can come up with that would let you do what you want.

If the IDs truly need to be static, then you'll need to define distinct static variables in each of the subclasses.

查看更多
爷的心禁止访问
7楼-- · 2019-01-18 11:26

static fields will not be inherited. every instance will use the same field. to accomplish what you want i would change the field into an instance field and either have one instance which internally increments this field or if you need to create three instances you could have a constructor where you pass the incremented value.

查看更多
登录 后发表回答