Setup: Java SE 6. Previous/future versions of Java may be different, I haven't tried them out yet.
If you declare a static variable, by default it doesn't get inherited:
Java Code
public class Base
{
public static int x = 5;
}
Java Code
public class Derived extends Base
{
static
{
x = 3; // this is changing Base's x
}
}
Java Code
public class Main
{
public static void main(String[] args)
{
System.out.println(Derived.x); // Will spit out an error, need to access the method statically, or from Base
System.out.println(Base.x); // prints out "3" because the static Derived got built second
}
}
However, if you have your Derived class declare a static variable x:
Java Code
public class Derived extends Base
{
public static int x; // may get a warning about variable hiding
static
{
x = 3; // Derived's x is change, Base x is still 5
}
}
Then you can use the two instance variables separate denoted by the class name. Note that from this method, it's not even necessary to force the two variables to the same type.
Java Code
public class Derivved extends Base
{
public static String x = "Hello"; // again, there may be a warning about variable hiding
}