There are some fuzzy operations exist on static variables when inherited, made a simple test:
class Test
{
private String mName;
public Test(String name) {
setName(name);
}
public void setName(String name) {
mName = name;
}
public String getName() {
System.out.println(mName);
return mName;
}
}
class A {
protected static String TAG = "A";
protected static Test mTest;
public A() {
}
public Test getTest() {
return mTest;
}
}
class B extends A
{
protected static String TAG = "B";
public B() {
mTest = new Test(TAG);
}
}
class C extends A
{
protected static String TAG = "C";
public C() {
mTest = new Test(TAG);
}
}
public class Demo
{
public static void main(String[] args) {
B b = new B();
C c = new C();
b.getTest().getName(); // print C
c.getTest().getName(); // print C
}
}
If there is a static variable in JAVA inherited superclass, subclasses, or operating with a static variable address, and therefore the operation is to subclass cover each other.
If the child class overrides the parent class methods and static variables are the exclusive sub-category, and the operation of other subclasses will not cover each other.
Reproduced in: https: //my.oschina.net/zhouz/blog/213133