Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 712
Anonymous Avatar asked Dec 29 '11 22:12

Anonymous


1 Answers

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.

like image 85
kosa Avatar answered Oct 03 '22 01:10

kosa