0

I have used lists in the past and got the same problem. I used this question to answer it. But when I made a new Abstract Class named Value and use it in the following way: public Value<Boolean> preferSwords = new Value<>("PreferSwords", true); I get the error. The Value class:

package Cupert.util;

import com.google.gson.JsonObject;



import java.util.function.Predicate;



public abstract class Value<T> {

    private String name;

    private T object;

    private T defaultVal;

    /**

     * The validator which is called every time the value is changed

     */

    private Predicate<T> validator;



    Value(String name, T defaultVal, Predicate<T> validator) {

        this.name = name;

        this.object = defaultVal;

        this.defaultVal = defaultVal;

        this.validator = validator;

    }



    public abstract void addToJsonObject(JsonObject obj);



    public abstract void fromJsonObject(JsonObject obj);



    public String getName() {

        return name;

    }



    public T getObject() {

        return object;

    }



    public boolean setObject(T object) {

        if (validator != null && !validator.test(object)) return false;



        this.object = object;



        return true;

    }



    public void setValidator(Predicate<T> validator) {

        this.validator = validator;

    }



    public Object getDefault() {

        return defaultVal;

    }

}
  • 1
    `Value` is an abstract class. Abstract classes cannot be instantiated with the `new` keyword. In order to assign `preferSwords` to some object of type `Value`, you will need to make a concrete class that extends `Value` and therefore implements its abstract members `addToJsonObject` and `fromJsonObject`. This can either be done via an explicit class declaration or an anonymous inner class. – ajc2000 Aug 04 '20 at 17:38
  • Ok thanks! Its kinda like how you cant use List list = new List<>() right? – comicalvery Aug 04 '20 at 17:40
  • 1
    Correct. `List` is an interface, which also cannot be instantiated. You would have to assign a `List` variable to an instance of a class that implements the List interface, such as `ArrayList` or `LinkedList`. – ajc2000 Aug 04 '20 at 17:41
  • Thanks can you just post that as an answer so I can tick it? – comicalvery Aug 04 '20 at 17:42

0 Answers0