In Java, if a child class shadows a static parent variable with an instance child variable, which variable will inherited methods use?

Anonymous picture Anonymous · Dec 29, 2011 · Viewed 9.5k times · Source

This is probably a bad thing to do, as discussed in Can parent and child class in Java have same instance variable?. (What if the parent variable name is changed? Then it will not be shadowed anymore.) However, I am still curious whether variables that are differently static/nonstatic will shadow each other. On one hand I would expect they are the same variable name so would be shadowed, but on the other hand it seems like the compiler might distinguish between the two based on staticness.

Answer

kosa picture kosa · Dec 30, 2011

As per Java language specification:

If the class declares a field with a certain name, then the declaration of that field is said to hide any and all accessible declarations of fields with the same name in superclasses, and superinterfaces of the class.

A hidden field can be accessed by using a qualified name (if it is static)

JVM Specification

You may refer "Field Declarations" section.