Are Static Fields Inherited

Java and inherited static members

As others already wrote, static members are bound to the class, so you need to track the id on a class level, e.g. like this:

abstract class Parent {
private int ID;

Parent() {
ID = nextId();
}

abstract protected int nextId();
}

class Sub1 extends Parent {
private static int curID = 0;

protected int nextId() {
return curID++;
}

//...
}

class Sub2 extends Parent {
private static int curID = 0;

protected int nextId() {
return curID++;
}

//...
}

Note that this approach is not thread safe - but neither was the code in the question. You must not create new objects from the same sub class concurrently from different threads.

Are static variables inherited

Please have a look into the documentation of oracle: http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#d5e12110

Static variables are inherited as long as they're are not hidden by another static variable with the same identifier.

How does the inheritance of static variables and their values work?

if the value of the static variable z is "shared" among the subclass(es)?

Yes. static variables are class level variables, meaning they do not belong to an instance of a class but to the class itself. They are accessible from a child class as long as they are not declared private, and they are not inherited. There's only one static variable for all instances of the class regardless if it's an instance of the class itself or an instance of any of its sub-classes. Thus:

A.z = 5; // z is 5 now
B.z = 7; // z is 7 now
System.out.prinln(A.z); // will print 7 since there's only one z shared by everybody
// remember, z belongs to A not to an instance of A.

When I declare b does this mean that first the constructor of A is used and passes the modified values of z to the constructor of B?

Not quite. Before a child class is constructed, its parent class needs to be constructed. How can you extend something that doesn't exist =]? Thus a call to the parent constructor is made before any other instruction in the child constructor.

public B() {
x++;
z++
}

is equivalent to

public B() {
super(); // initialize A before initializing B
x++;
z++
}

If you don't call super() explicitly, which unless you need to pass something to the parent constructor you don't have to, the compiler will insert super() for you. So in the B constructor, the constructor for A is called but it doesn't pass z to B it just does what you instructed it to do and moves on.

Are static fields inherited?

3 in all cases, since the static int total inherited by SomeDerivedClass is exactly the one in SomeClass, not a distinct variable.

Edit: actually 4 in all cases, as @ejames spotted and pointed out in his answer, which see.

Edit: the code in the second question is missing the int in both cases, but adding it makes it OK, i.e.:

class A
{
public:
static int MaxHP;
};
int A::MaxHP = 23;

class Cat: A
{
public:
static const int MaxHP = 100;
};

works fine and with different values for A::MaxHP and Cat::MaxHP -- in this case the subclass is "not inheriting" the static from the base class, since, so to speak, it's "hiding" it with its own homonymous one.

Inheritance of something like a static field

It's not possible to make static members abstract/virtual, because polymorphism is based on the actual type of the instance, and static members don't belong to a specific instance.

I think this satisfies all your requirements:

abstract class A
{
public abstract Image Symbol{get;}
}
class B:A
{

private static readonly Image _symbol = ...

public override Image Symbol{get{return _symbol;}}
}
  • each derived class has to implement Symbol, since it's abstract in the base class
  • the static field is readonly, so you can't change it after initialization
  • the instance doesn't contain the image instance, it's shared across all instances of the class
  • (not sure what you mean about the last one, though...)

Of course, the downside is that it requires all derived class to adhere to this pattern. This doesn't prevent a derived class from violating some of the requirements.

Are static methods inherited in Java?

All methods that are accessible are inherited by subclasses.

From the Sun Java Tutorials:

A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members

The only difference with inherited static (class) methods and inherited non-static (instance) methods is that when you write a new static method with the same signature, the old static method is just hidden, not overridden.

From the page on the difference between overriding and hiding.

The distinction between hiding and overriding has important implications. The version of the overridden method that gets invoked is the one in the subclass. The version of the hidden method that gets invoked depends on whether it is invoked from the superclass or the subclass

Java Static Variables and inheritance and Memory

If I have a couple subclasses inheriting some static field from their
superclass, will they share the class variables or not?

Yes They will share the same class variables throughout the current running Application in single Classloader.
For example consider the code given below, this will give you fair idea of the sharing of class variable by each of its subclasses..

class Super 
{
static int i = 90;
public static void setI(int in)
{
i = in;
}
public static int getI()
{
return i;
}
}
class Child1 extends Super{}
class Child2 extends Super{}
public class ChildTest
{
public static void main(String st[])
{
System.out.println(Child1.getI());
System.out.println(Child2.getI());
Super.setI(189);//value of i is changed in super class
System.out.println(Child1.getI());//same change is reflected for Child1 i.e 189
System.out.println(Child2.getI());//same change is reflected for Child2 i.e 189
}
}


Related Topics



Leave a reply



Submit