简体   繁体   English

无法更正我的Java程序

[英]Can't correct my java program

I just started learning java and I'm working on a program. 我刚开始学习Java,正在研究程序。 I'm getting an error here: 我在这里遇到错误:

locationsOfCells = simpleDotCom.getLocationCells();

but I'm not sure what the error is. 但我不确定是什么错误。 Eclipse say Eclipse说

Cannot make a static reference to the non-static method getLocationCells() from the type simpleDotCom 无法从类型simpleDotCom静态引用非静态方法getLocationCells()

Can someone help me with this? 有人可以帮我弄这个吗? What am I doing wrong? 我究竟做错了什么?

public class simpleDotCom {
    int[] locationsCells;

    void setLocationCells(int[] loc){
        //Setting the array
        locationsCells = new int[3];
        locationsCells[0]= 3;
        locationsCells[1]= 4;
        locationsCells[2]= 5;
    }

    public int[] getLocationCells(){

        return locationsCells;

    }
}

public class simpleDotComGame {

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

    private static void printBoard(){
        simpleDotCom theBoard = new simpleDotCom();
        int[] locationsOfCells; 
        locationsOfCells = new int[3];
        locationsOfCells = theBoard.getLocationCells();

        for(int i = 0; i<3; i++){
            System.out.println(locationsOfCells[i]);
        }

    }

}

The problem is you are calling the getLocationCells() method as if it was a static method when in fact it is an instance method. 问题是您实际上在调用getLocationCells()方法,就像它是一个静态方法一样,而实际上它是一个实例方法。

You need to first create an object from your class like this: 您需要首先从您的类中创建一个对象,如下所示:

simpleDotCom myObject = new simpleDotCom();

and then call the method on it: 然后在其上调用方法:

locationsOfCells  = myObject.getLocationCells();

Incidentally, there is a widely followed naming convention in the Java world, where class names always start with a capital letter - you should rename your class to SimpleDotCom to avoid confusion. 顺便说一句,在Java世界中,有一个广泛遵循的命名约定,其中类名始终以大写字母开头-您应将类重命名为SimpleDotCom以避免混淆。

You are attempting getLocationCells in a static way. 您正在以静态方式尝试getLocationCells You need to create an instance of simpleDotCom first: 您需要首先创建simpleDotCom的实例:

simpleDotCom mySimpleDotCom = new simpleDotCom();       
locationsOfCells = mySimpleDotCom.getLocationCells();

BTW class names always start with a capital letter. BTW类别名称始终以大写字母开头。 This would help remove the confusion of accessing the method as a member method. 这将有助于消除将方法作为成员方法访问的困惑。

Update: 更新:

To access from your updated static method, you would need to declare theBoard as a static variable also: 要从更新后的静态方法访问,还需要将theBoard声明为static变量:

static simpleDotCom theBoard = new simpleDotCom();

you are trying to reference a non static method from the main method. 您正在尝试从main方法引用非静态方法。 That is not permitted in java. 在Java中是不允许的。 You can try making that simpleDotCom Class as static, so that u can have access to the methods of that class. 您可以尝试将simpleDotCom类设为静态,以便您可以访问该类的方法。

simpleDotCom obj = new simpleDotCom();
locationsOfCells = obj.getLocationCells();

And also your class name should start with a capital letter 而且您的班级名称也应该以大写字母开头

You are trying to access a normal non static method from a static context, it does not work. 您正在尝试从静态上下文访问普通的非静态方法,该方法不起作用。

You can either remove the static word from the routine where you try to access the: getLocationCells() from, or make getLocationCells() static by adding the static word in its declaration. 您可以从尝试访问getLocationCells()的例程中删除该静态词,也可以通过在其声明中添加静态词来使getLocationCells()成为静态。

也可以使simpleDotCom的字段和方法静态化,或者创建simpleDotCom的实例并访问该实例的方法。

Your code have some more errors. 您的代码还有更多错误。

  1. The non static method couldn't call up with class name. 非静态方法无法使用类名调用。 So try to call the getLocationCells() with object. 因此,请尝试使用对象调用getLocationCells()。

    simpleDotCom obj=new simpleDotCom(); simpleDotCom obj = new simpleDotCom(); obj.getLocationCells() obj.getLocationCells()

  2. Next u will get the null pointer exception. 接下来,您将获得空指针异常。 U try to print the locationsOfCells values before it is initialized. U尝试在初始化之前打印locationsOfCells值。 So try call the setLocationCells() method before printing values. 因此,尝试在打印值之前调用setLocationCells()方法。

  3. Ur method definition void setLocationCells(int[] loc). Ur方法定义void setLocationCells(int [] loc)。 Here u having the parameter loc but u didn't use any where in the method block. 在这里,您具有参数loc,但是您在方法块中未使用任何位置。 So please aware of handling method parameter. 因此请注意处理方法参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM