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

    }

}

Answers


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.


Need Your Help

Sort user defined multi-dimensional array

php arrays sorting

I have a user defined multidimensional array that I am trying to sort. I looked around, and say only how to sort single dimensional arrays, or associative arrays with keys... So, if I have the

Django default value date and time when form opens up

python django django-models

Hi guys I'm trying to pre-populate a date time field when my user opens the form. I need to display the datetime field too as the user may need to change it.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.