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.
-
1I feel that this is something important to consider and may help others, so this question is of use, but: **Have you tried it?** – Zéychin Dec 29 '11 at 23:08
-
4Trying it is one thing, but that may be compiler dependent. I'm wondering if there is a definitive rule about this in the language specification. – Anonymous Dec 29 '11 at 23:14
6 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)
You may refer "Field Declarations" section.

- 65,990
- 13
- 130
- 167
-
So far this is one of the best answers, but it seems to focus only on shadowing. I wouldn't remove any information, but is there anything additional in the specification that would indicate that inherited methods also use the child's fields? – Anonymous Dec 30 '11 at 00:59
-
-
+1 to this answer because it mentions that you can still access the field via fully qualified name. – BillRobertson42 Dec 30 '11 at 06:21
they will:
class Parent {
static String x="static in parent";
String y="instance in parent";
}
class Child extends Parent {
static String y="static in child";
String x="instance in child";
void foo() {
System.out.println("x "+x);
System.out.println("super.x " + super.x);
System.out.println("y "+y);
System.out.println("super.y " + super.y);
}
}
public class Main {
public static void main(String[] args) {
Parent parent=new Parent();
Child child=new Child();
System.out.println("Parent.x "+Parent.x);
System.out.println("parent.x "+Parent.x);
System.out.println("parent.y "+parent.y);
System.out.println("child.x "+child.x);
System.out.println("Child.y "+Child.y);
System.out.println("child.y "+child.y);
System.out.println("(Parent)child).x "+((Parent)child).x);
System.out.println("(Parent)child).y "+((Parent)child).y);
child.foo();
}
}
Parent.x static in parent
parent.x static in parent
parent.y instance in parent
child.x instance in child
Child.y static in child
child.y static in child
(Parent)child).x static in parent
(Parent)child).y instance in parent
x instance in child
super.x static in parent
y static in child
super.y instance in parent

- 9,841
- 8
- 50
- 90
-
1Post the output too, so people can learn what it does, either as in-line comments or the output pasted in below – Bohemian Dec 30 '11 at 00:29
-
I understand the idea of shadowing, but the important part of my qusetion is "which variable will inherited methods use?". – Anonymous Dec 30 '11 at 00:58
-
Very nice. A combination of this answer and @thinksteep's answer would be better, and this plus a reference to the language specification where it says definitively that this is the correct behavior would be ideal. (Thinksteep's answer sort of says this but doesn't mention inherited methods specifically.) However, I'll accept this answer until any major improvements are made in other answers. – Anonymous Dec 30 '11 at 02:31
-
Hmm, the problem I see here is that we're calling a child method, not a parent method, to test what they are. I'll do that, just a moment... – Anonymous Dec 30 '11 at 02:53
-
the parent method will just access stuff in the parent whether or not it is a parent or a child. – Ray Tayek Dec 30 '11 at 03:02
-
Right, but how do you know it will do that for sure? I see that it does with my compiler (Eclipse), but is that specified somewhere? (Also, see my answer that has parent.bar().) – Anonymous Dec 30 '11 at 03:06
-
the parent class has *no* knowledge about any derived class that someone might make. how could it possibly know? – Ray Tayek Dec 30 '11 at 03:08
-
I was thinking that it might be possible that the child class could manipulate which variables were used by overriding them. But yeah, this does seem less reasonable the more I think about it. I was thinking of it more like nested blocks of code, where a more deeply nested block could manipulate the variables in the outer block. But I suppose the methods in the two classes have completely different scope. The strange part about this is being able to call methods defined in the other scope but not change the variables. Although if they're declared private that makes sense. – Anonymous Dec 30 '11 at 03:11
-
The answer to this question becomes obvious if I specify it more completely, as in "will inherited methods use *private* member variables from the parent or child; what about *protected*". Clearly the private variable is not visible to the child and so lexically the parent method must use the only variable visible to it (in the parent). If protected, it is a non-issue because it's the same variable. Changing one changes the other. (Unless you define a new one in which case again it makes sense that it wouldn't be visible to the parent method.) – Anonymous Dec 30 '11 at 03:17
From The Java Language Specification:
If an expression name consists of a single Identifier, then there must be exactly one visible declaration denoting either a local variable, parameter or field in scope at the point at which the the Identifier occurs. Otherwise, a compile-time error occurs.
If the declaration declares a final field, the meaning of the name is the value of that field. Otherwise, the meaning of the expression name is the variable declared by the declaration.
If a method in a superclass refers to a particular field (static or otherwise) of that class, only that class's declaration of the field will be in scope at that point; any fields (static or otherwise) of subclasses will not be in scope. Therefore, the method will always use the superclass field, even if a subclass inherits it and shadows that field.
This answer is completely rewritten based on my new understanding of the question. Below is my first answer, kept for posterity.
From The Java Language Specification:
A declaration d of a field, local variable, method parameter, constructor parameter or exception handler parameter named n shadows the declarations of any other fields, local variables, method parameters, constructor parameters or exception handler parameters named n that are in scope at the point where d occurs throughout the scope of d.
This suggests that compilers are required to shadow parent variables, regardless of staticness.
Note that none of this is relevant to inherited methods, which always use the original variables regardless of whether a subclass shadows them. I suspect this isn't what you meant to ask.

- 24,950
- 9
- 62
- 84
-
1This is actually an answer to what I was trying to ask: "none of this is relevant to inherited methods, which always use the original variables regardless of whether a subclass shadows them". – Anonymous Dec 30 '11 at 00:55
-
Does the language specification say this? If so, where? I am trying to find it. – Anonymous Dec 30 '11 at 00:56
-
Look at the code below. If you want access field
from the ChildClass
, it will use its own member variable. If you want to access the static field
from SuperClass
, you have to call it explicitly by using SuperClass.field
. The `STATIC_FIELD`` can be accessed directly since there is no ambiguity to the compiler.
public class SuperClass{
static String field = "staticField1";
static String STATIC_FIELD = "staticField2";
//not possible to have a member field in this class -> compile error
//String field = "memberField"; is not possible
public SuperClass() {
System.out.println( "field = " + field );
}
}
public class ChildClass extends SuperClass{
String field = "memberField";
public ChildClass() {
System.out.println( "field = " + field );//access to member field
//need to explicitly call SuperClass.field to access the static variable
System.out.println( "SuperClass.field = " + SuperClass.field );
//no need to do this when there is no amibiguity
System.out.println( "STATIC_FIELD = " + STATIC_FIELD );
}
}

- 36,233
- 5
- 47
- 99
-
I understand the idea of shadowing, but the important part of my qusetion is "which variable will inherited methods use?". – Anonymous Dec 30 '11 at 00:58
public class Test {
public static int MYNUMBER = 5;
public Test() {
}
}
public class TestChild extends Test {
public int MYNUMBER = 8;
public TestChild() {
}
}
public class Main {
public static void main(String[] args) {
TestChild testChild = new TestChild();
// This would print out 8
System.out.println("in main test child: " + testChild.MYNUMBER);
// This won't even compile if the MYNUMBER variable is overshadowed
// If the MYNUMBER variable is not overshadowed it
// would compile and
// print out 5
// If we make MYNUMBER static also on TestChild,
// it would compile and print out 8
System.out.println("in main TestChild.MYNUMBER " + TestChild.MYNUMBER);
// This would print out 5
System.out.println(Test.MYNUMBER);
}
}

- 178
- 2
- 9
-
I understand the idea of shadowing, but the important part of my qusetion is "which variable will inherited methods use?". – Anonymous Dec 30 '11 at 00:58
-
The inherited methods will use the static variable of the parent. So if we add to the Test class the method: public int doSomething(){ return MYNUMBER; } doSomething will return 5 at all times. – Alex Dec 30 '11 at 20:58
With Eclipse's compiler, methods from the parent class will still use the member variables from the superclass, not the shadowed variables from the child class, as shown below. This is an only slightly modified version of thinksteep's answer.
class Parent {
static String x ="static in parent";
String y="instance in parent";
void bar() {
System.out.println("x "+ x);
System.out.println("y "+ y);
}
}
class Child extends Parent {
static String y ="static in child";
String x="instance in child";
void foo() {
System.out.println("x "+x);
System.out.println("super.x " + super.x);
System.out.println("y "+y);
System.out.println("super.y " + super.y);
}
}
public class Main {
public static void main(String[] args) {
Parent parent=new Parent();
Child child=new Child();
System.out.println("Parent.x "+Parent.x);
System.out.println("parent.x "+Parent.x);
System.out.println("parent.y "+parent.y);
System.out.println("child.x "+child.x);
System.out.println("Child.y "+Child.y);
System.out.println("child.y "+child.y);
System.out.println("(Parent)child).x "+((Parent)child).x);
System.out.println("(Parent)child).y "+((Parent)child).y);
System.out.println("Member variable visibility in parent:");
parent.bar();
System.out.println("Member variable visibility in child:");
child.foo();
System.out.println("Member variable visibility in inherited methods:");
child.bar();
System.exit(0);
}
/* Output:
Parent.x static in parent
parent.x static in parent
parent.y instance in parent
child.x instance in child
Child.y static in child
child.y static in child
(Parent)child).x static in parent
(Parent)child).y instance in parent
Member variable visibility in parent:
x static in parent
y instance in parent
Member variable visibility in child:
x instance in child
super.x static in parent
y static in child
super.y instance in parent
Member variable visibility in parent methods:
x static in parent
y instance in parent
*/

- 3,334
- 3
- 35
- 50