简体   繁体   中英

Can't correct my java program

I just started learning java and I'm working on a program. I'm getting an error here:

locationsOfCells = simpleDotCom.getLocationCells();

but I'm not sure what the error is. Eclipse say

Cannot make a static reference to the non-static method getLocationCells() from the type simpleDotCom

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.

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.

You are attempting getLocationCells in a static way. You need to create an instance of simpleDotCom first:

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

BTW class names always start with a capital letter. 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:

static simpleDotCom theBoard = new simpleDotCom();

you are trying to reference a non static method from the main method. That is not permitted in java. You can try making that simpleDotCom Class as static, so that u can have access to the methods of that class.

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.

也可以使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.

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

  2. Next u will get the null pointer exception. U try to print the locationsOfCells values before it is initialized. So try call the setLocationCells() method before printing values.

  3. Ur method definition void setLocationCells(int[] loc). Here u having the parameter loc but u didn't use any where in the method block. So please aware of handling method parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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