Java 非静态方法 addInv(int) 无法从静态上下文中引用

标签 java static compiler-errors

我很清楚这个错误,但这是我第一次被难住。我知道我不能从主方法调用非静态变量,但这很令人困惑。也许你能帮忙?该错误显示在 addInv(1); 行...

代码:

import java.io.*;
import java.util.*;
import javax.swing.*;

public class item
{
    public static int attack, defense;
    public static ArrayList<String> arr;
public static String name, desc, typeOf, attackAdd, defenseAdd, canSell, canEat,earnedCoins,canEquip;

    String stats[];

        public static void main(String args[])
        {

            addInv(1);

        }

public void addInv(int e) {

String iname = getItem(1)[0];

String idesc = getItem(1)[1];
int itypeOf = Integer.parseInt(getItem(1)[2]);
int iattackAdd = Integer.parseInt(getItem(1)[3]);
int idefenseAdd = Integer.parseInt(getItem(1)[4]);
boolean icanSell = Boolean.parseBoolean(getItem(1)[5]);
boolean icanEat = Boolean.parseBoolean(getItem(1)[6]);
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) {

        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};

    }

}

谢谢。

最佳答案

您不能从静态方法调用非静态方法。因此,要么将 addInv 设为静态:

public class Item {
    ...

    public static void main(String args[]) {
        addInv(1);
    }

    public static void addInv(int e) {
        ...
    }

    public static String[] getItem(int e) {
        ...
    }
}

但是将所有这些方法设为静态可能不是一个合适的设计,您可能更喜欢在 main 中使用 Item 实例:

public class Item {
    ...

    public static void main(String args[]) {
        Item item = new Item();
        item.addInv(1);
    }

}

顺便说一句,我并不是有意无礼,但你应该认真研究你的(糟糕的)代码格式和约定,无论是为了读者还是你自己。

另请参阅

关于Java 非静态方法 addInv(int) 无法从静态上下文中引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3272624/

相关文章:

java - Java虚拟机反复崩溃怎么办?

Java jackson : deserialize complex polymorphic object model: JsonMappingException: Unexpected token (START_OBJECT), 应为 VALUE_STRING

java - 向 Spring ResponseEntity 添加一个新的 Header

java - 捕获图像,上传到 Firebase 并检索 Java Android Studio

c++ - 动态分配数组和静态数组的区别

c# - 正确使用共享静态 'Random' 单例实例?

Eclipse上Java编译错误

java - 用静态方法反转数组 VS static void?

c++ - Qt Creator编译器配置问题visual studio 2017

c++ - 一段简单的 C++ 代码中的错误 C2280