1
public abstract class Test {

    private static int value = 100;

}

And

public abstract class Test {

    private int value = 100;

}

Since Test is abstract, it can't be instantiated, and therefore it doesn't make any difference whether value is static or not, right?

Is there any difference when a field is static or not when it belongs to an abstract class?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Saturn
  • 17,888
  • 49
  • 145
  • 271

3 Answers3

2

Yes, there is. Even thou your class is abstract, it can have non-abstract non-static methods working with non-static private fields. It is usefull sometimes.

Dummy exaple:

Consider following: you want to hold one integer and give everyone the ability to change it, but you dont want them to set negative values, or values bigger then 15, but the condition isn't known (in general) by everyone, so you want to ensure that when someone sets incorect value, it gets fixed automaticly.

Here is one possible solution:

abstract class MyInt {

    private int myInt;

    public int getMyInt() {
        return myInt;
    }

    public void setMyInt(int i) {
        myInt = checkMyInt(i);
    }

    protected abstract int checkMyInt(int i);
}

Now you can inplement any logic in checkMyInt() and hand over the instance declared as MyInt

pastebin exaplme

PS: this probably isnt the best solution and i would use interfaces here, but as an example it is enought i hope

kajacx
  • 12,361
  • 5
  • 43
  • 70
1

Abstract classes can't be instantiated directly. But the whole point of abstract classes is to have subclasses that are instantiated:

public abstract class Test
    protected int value;
}

public class TestImpl extends Test {
    public TestImpl(int value) {
        this.value = value;
    }
}

In the above example, each instance of TestImpl (and thus of Test) has its own value. With a static field, the field is scoped to the Test class, and shared by all instances.

The difference between static and non-static fields is thus exactly the same as with any other non-abstract class.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Yes, but here you use `protected` fields, which are quite normal when working with abstract classes, i think the question was about `private` fields. – kajacx Jan 12 '14 at 13:39
  • It's the same if the field is private. Every instance will have its own value. It will only be read/modified by the methods defined in base class, that's all. Both examples posted by the OP are too simplified: there is no point in defining a private field, abstract or not, that is never read nor written. – JB Nizet Jan 12 '14 at 13:40
  • I removed my initial comment as I figured out after posting that both fields where declared private and [private fields do not get inherited](http://stackoverflow.com/questions/6543328/private-members-in-java-inheritance) - so my initial comment was wrong concerning the question. You are however right that the methods defined in the abstract class have access to this field. Though static fields should only be accessed in static context - and static methods do not get inherited to child classes. The compiler gives appropriate warnings when accessing static fields in non static context. – Roman Vottner Jan 12 '14 at 13:47
0

An abstract class is a normal (base) class, just declared to be missing some things, like abstract methods.

So there is definite a difference.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138