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

前端 未结 4 1477
有刺的猬
有刺的猬 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:58

    static in Java means that it is a class variable or method, meaning that the method or variable is shared across all instances of the class. This also implies that you do not need to instantiate the class to call the method. NOTE: Unless you have a good reason to make things static it is generally not a good idea, because it makes a bunch of global variables and functions.

    The proper way to address this is to create an instance of your class in the main and then call the method on the instance.

    NOTE2: static variables run into issues if you have multiple threads and this means you need to synchronize the reads and writes across these Threads.

    I would look at this: http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/java/javaOO/classvars.html

    EDIT: Here is the code without the statics, that seems to work.

    import java.util.ArrayList;
    
    public class Item 
    {
        public int attack, defense;
        public ArrayList arr = new ArrayList();
        public String name, desc, typeOf, attackAdd, defenseAdd, canSell,
                canEat, earnedCoins, canEquip;
    
        String stats[];
    
        public static void main(String args[]) 
        {
            Item anItem = new Item();
            anItem.addInv(1);
        }
    
        public void addInv(int e) 
        {
            String iname = getItem(1)[0];
            String idesc = getItem(1)[1];
    
            // Never read
            //int itypeOf = Integer.parseInt(getItem(1)[2]);
            int iattackAdd = Integer.parseInt(getItem(1)[3]);
            int idefenseAdd = Integer.parseInt(getItem(1)[4]);
            // Never read
            //boolean icanSell = Boolean.parseBoolean(getItem(1)[5]);
            // Never Read
            //boolean icanEat = Boolean.parseBoolean(getItem(1)[6]);
            // Never Read
            //int iearnedCoins = Integer.parseInt(getItem(1)[7]);
    
            attack = attack + iattackAdd;
            defense = defense + idefenseAdd;
            System.out.println("You picked up: " + iname);
            arr.add("dan");
            System.out.println("Description: " + idesc);
    
        }
    
        // Types of Items
        // 0 - Weapon
        // 1 - Food
        // 2 - Reg Item
        // 3 - Helmet
        // 4 - Armor Legs
        // 5 - Chest Armor
        // 6 - Shield
    
        public String[] getItem(int e) 
        {
            // Never read
            // String[] stats = new String[7];
            String name = "Null";
            String desc = "None";
            String typeOf = "0";
            String attackAdd = "0";
            String defenseAdd = "0";
            String canSell = "true";
            String canEat = "false";
            String earnedCoins = "0";
    
            if (e == 1) {
    
                name = "Pickaxe";
                desc = "Can be used to mine with.";
                typeOf = "2";
                attackAdd = "2";
                earnedCoins = "5";
            }
    
            return new String[] { name, desc, typeOf, attackAdd, defenseAdd,
                    canSell, canEat, earnedCoins };
        }
    }
    

提交回复
热议问题