Java non-static method addInv(int) cannot be referenced from a static context

前端 未结 4 1462
有刺的猬
有刺的猬 2021-01-26 08:09

I know this error very well however this is the once time I\'m stumped. I know that I can\'t call non-static variables from main method but this is confusing. Maybe you can help

4条回答
  •  醉梦人生
    2021-01-26 08:49

    You cannot call a method in the main, that is not associated with an object like that. You needed to call:

    item.addInv(1);

    which means that your public void addInt(int e) would need to be static:

    public static void addInt(int e)  
    

    This means that your implementation of that method will need to be changed, because you are mixing some concepts there.

    Basicly, static methods are methods that belong to the class they are in, and so they are not particular to any object of that class. Such methods can be called using the class name followed by a dot and the method name, and their implementation should not use specific object values, since those methods themselves are not associated with any particular objects from the class.

    Hope that helps

提交回复
热议问题