Why can final object be modified?

Matt McCormick picture Matt McCormick · Mar 12, 2010 · Viewed 60.2k times · Source

I came across the following code in a code base I am working on:

public final class ConfigurationService {
    private static final ConfigurationService INSTANCE = new ConfigurationService();
    private List providers;

    private ConfigurationService() {
        providers = new ArrayList();
    }

    public static void addProvider(ConfigurationProvider provider) {
        INSTANCE.providers.add(provider);
    }

    ...

INSTANCE is declared as final. Why can objects be added to INSTANCE? Shouldn't that invalidate the use of final. (It doesn't).

I'm assuming the answer has to do something with pointers and memory but would like to know for sure.

Answer

Sean Owen picture Sean Owen · Mar 12, 2010

final simply makes the object reference unchangeable. The object it points to is not immutable by doing this. INSTANCE can never refer to another object, but the object it refers to may change state.