Does static variable inherited? [duplicate]

2019-09-21 02:20发布

This question already has an answer here:

We know that in java static variable are not inherited. But in below code I am not getting any error as I want to initialize the static variable in child class.

class s
{
    static int x;
}

class aaa extends s
{

    void fun()
    {
        x=2;
        System.out.println(x);
    }

    public static void main(String args[])
    {

        aaa w=new aaa();
        w.fun();

    }
}

标签: java static
1条回答
家丑人穷心不美
2楼-- · 2019-09-21 02:50

static members are most definitely accessible from subclasses, as your example shows. You cannot override them, of course, but you could hide them.

查看更多
登录 后发表回答