Does static variable inherited? [duplicate]

2019-09-21 02:16发布

问题:

This question already has an answer here:

  • Are static variables inherited 3 answers

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();

    }
}

回答1:

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



标签: java static