Unchanging collection adding strings

Asked

Viewed 54 times

0

I’m starting java programming and I’m having a problem creating an immutable collection. Below is what I created:

package br.com.estudos;

import java.util.Collection;


public class MinhaColecaoImutavel<String> extends MinhaColecao<String> {


    public static MinhaColecaoImutavel instancia;


    public static synchronized MinhaColecaoImutavel getinstancia(Collection<?> colecao) {
        if (instancia == null) {
            instancia = new MinhaColecaoImutavel(colecao);
        }

        return instancia;
    }

    private MinhaColecaoImutavel(Collection<String> colecao) {
        instancia.addAll(colecao);
    }

}

When executing main by the command below,

MinhaColecaoImutavel<String> x = MinhaColecaoImutavel.getinstancia(new ArrayList<>());
        x.add("x");

Give me the error NullPointerException on the line "instancia.addAll(colecao)". My x is to add a value in the instance so that it is not null.

Someone has a light to help?

Thank you.

1 answer

1

This static field means that it is impossible to use two instances of MinhaColecaoImutavel. This is a bad idea, after all, there should be no problem in creating two distinct instances of this class in different locations for different purposes. For example, at one location I create an immutable collection with printer names and another part of totally different code I create an immutable list with words read from a dictionary. With this code, the same list will be returned in both cases, causing confusion, pain and suffering. The static should not be used in this way.

The solution is simple. Forget the static and the static method and use the constructor directly:

package br.com.estudos;

import java.util.Collection;

public class MinhaColecaoImutavel<String> extends MinhaColecao<String> {
    public MinhaColecaoImutavel(Collection<String> colecao) {
        this.addAll(colecao);
    }
}

And you wear it like this:

MinhaColecaoImutavel<String> x = new MinhaColecaoImutavel(new ArrayList<>());
x.add("x");

However, this is still not right. Note that the class MinhaColecaoImutavel adds nothing to the superclass but a constructor. Once an instance of the subclass has been built, it has nothing other than what is offered by the superclass. This means that this is a misuse of inheritance that brings no benefit whatsoever. The solution is to put the constructor in the superclass (I’m assuming the generic type is T):

    public MinhaColecao(Collection<T> colecao) {
        this.addAll(colecao);
    }

And you wear it like this:

MinhaColecao<String> x = new MinhaColecao(new ArrayList<>());
x.add("x");

If your idea is to make an immutable subclass out of a mutable superclass, that’s not a good idea. The best would be to have an interface defining your collection and have two distinct implementations, one changeable and one immutable. The use of concrete methods with the modifier default on the interface (allowed from Java 8) should help you simplify the code of the implementations.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.