Java non-static method addInv(int) cannot be refer

2020-05-09 05:35发布

问题:

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? That error is show on addInv(1); line...

Code:

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

    }

}

Thanks.

回答1:

You can't call a non static method from a static method. So either make addInv static:

public class Item {
    ...

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

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

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

But making all these methods static may not be an appropriate design, you might prefer using an Item instance in your main:

public class Item {
    ...

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

}

By the way, I don't mean to be rude but you should seriously work on your (awful) code formatting and conventions, both for readers and for you.

See also

  • Code Conventions for the Java Programming Language
  • How to Write Doc Comments for the Javadoc Tool


回答2:

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


回答3:

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



回答4:

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<String> arr = new ArrayList<String>();
    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 };
    }
}