-2

I'm trying my hand at Generics and implementing a generic list in a simple text based UI, however I keep getting messages saying that I can't get to different methods etc from my methods that are and aren't static.

Can somebody please point out what I am doing wrong?

public class GenericList<T> {

    List<T> genericList = new ArrayList<T>();

    public void addGeneric(T t) {
        this.genericList.add(t);
    }

    public static void initial() {
        // USER INPUT AREA
        switch(ans.toLowerCase()) {
            case "add" : addToList(); break;
            case "delete" : deleteFromList(); break;
            case "print" : printList(); break;
        }
    }

    public void deleteFromList() {}

    public void printList() {
        for (int i = 0; i<this.genericList.size(); i++) {
            System.out.println(this.genericList.get(i));
        }
    }

    public void addToList() {
        // USER INPUT AREA
        switch(type.toLowerCase()) {
            case "x":
                System.out.println("Exitiing...");
                initial();
                break;
            case "string": type="String"; break;
            case "int": type="Integer"; break;
            case "integer": type="Integer"; break;
        }

        System.out.println("Enter data...");
        String data = s.next();
        if (data.equals("x")){
            System.out.println("Exiting...");
            initial();
        }
        initial();

    }

    public static void main(String[] args) {
        GenericList g = new GenericList();
        initial();

    }
}
physicsboy
  • 5,656
  • 17
  • 70
  • 119
  • `"I can't get to different methods etc from my methods that are and aren't static"` - Is that *exactly* what the error message states? What is the specific error message, and what specific line of code does it refer to? – David Nov 14 '17 at 17:02
  • You have declared `GenericList` to take a type parameter, but in `main` you have not supplied one. That's a raw type. Don't do that. – Elliott Frisch Nov 14 '17 at 17:03
  • Possible duplicate of [What does the 'static' keyword do in a class?](https://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-a-class) – OH GOD SPIDERS Nov 14 '17 at 17:04
  • Why are you creating an instance of `GenericList`, then throw it away and use the static `initial` method? Make `intial` non-static and call it from the created instance instead. – QBrute Nov 14 '17 at 17:32

2 Answers2

0

Your problem isn't generics. Change

public static void initial() {

to

public void initial() {

and change yout main to

public static void main(String[] args) {
    new GenericList().initial();
}
Leo Aso
  • 11,898
  • 3
  • 25
  • 46
  • 1
    [Raw Types](https://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html). Please do not encourage that. – Elliott Frisch Nov 14 '17 at 17:04
  • @ElliottFrisch you're absolutely right. But honestly the OP's code is too messed up to get into, so I just gave a solution for the immediate problem. – Leo Aso Nov 14 '17 at 17:07
0

I believe the issue has to do with the fact that you are using non-static methods (i.e. addToList() etc) within you static method (i.e. initial()). @danben's answer in the following post explains it pretty well: calling non-static method in static method in Java. To account for this you could make the other methods static or make initial() non-static.

El Fufu
  • 66
  • 1
  • 9