-3

When i try to use the move i.e. Slide.move(1,0) i get the error: non-static method move(int,int) cannot be referenced from a static context

This is my code at the moment, which i have no idea whats wrong.

    public void move(int row, int col) {
        char [][] temp= new char [cells.length][]; 
        for (int i= 0; i< cells.length; i++) {
            int destRow = (i+row)%cells.length;
            temp[destRow] = new char [cells[i].length];
            for (int j= 0; j < cells[i].length; j++)
                temp[destRow][(j+col)%cells[i].length] = cells[i][j];
            }
              cells= temp;
        }

    }   
Jay
  • 25
  • 7
  • 1
    Key is which line of code causes the compiler error to occur. I'm wondering if it's in the main method which you're not showing us. – Hovercraft Full Of Eels Oct 25 '13 at 22:11
  • 1
    Are you calling this method from the `main` method? If yes then to do so you must create a `static-reference` between both the `main-method` and this `method`. – Tdorno Oct 25 '13 at 22:11
  • Or main can create an instance of the class and call move on the instance. – Lee Meador Oct 25 '13 at 22:12
  • It would help if you showed the "context", and also split this into two separate questions. – Jason C Oct 25 '13 at 22:12
  • In short, you have to show us the code where `move` is called to get more info. – Lee Meador Oct 25 '13 at 22:13
  • I've looked at other questions but don't really get the theory behind it. I've just started learning java programming in university this semester and a bit behind in some parts. Would you be able to point me somewhere that i could read a bit more on these things? – Jay Oct 25 '13 at 22:13
  • I can post all the code, but i don't have a main method yet. For this project i have to do it in order where Task one doesn't consist of a main method. Task 2 is the one where i make the main method.. – Jay Oct 25 '13 at 22:14
  • http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html – Jason C Oct 25 '13 at 22:26

2 Answers2

3

The error means exactly what it says.

Your move method is not static. It requires an instance of an object to be called, e.g.:

Slide s = new Slide();
s.move(...);

You are calling it, as it says, from a static context, i.e. a place with no this, perhaps another static method or directly via Slide.move(...). You will need to not do that.

These fail:

Slide.move(...); // error: move requires an instance

// and

class Slide {
    void move (...) { 
        ... 
    }
    static void method () {
        move(...); // error: method is static, there is no instance
    }
}

These do not fail:

Slide s = new Slide(); 
s.move(...);

// or

class Slide {
    void move (...) { 
        ... 
    }
    void method () {
        move(...);
    }
}

// or

class Slide {
    static void move (...) { 
        ... 
    }
    static void method () {
        move(...);
    }
}

So either call it from a non-static context, or make it a static method.

As for recommending somewhere to read more about these things (which you asked in a comment), try the official Java tutorial at http://docs.oracle.com/javase/tutorial/ (take a look at the "Learning the Java Language" trail (in particular: http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html).

As for your output problems, since that's a separate question, you'd want to post a separate question for it.

Jason C
  • 38,729
  • 14
  • 126
  • 182
  • I don't have any static methods in my code... I'll just edit the first post with all the code to show you...? – Jay Oct 25 '13 at 22:26
  • Then you are calling it as `Slide.move(...)`, as I also said. You need to read the linked tutorial. – Jason C Oct 25 '13 at 22:27
  • I see your edit. You would need to clearly indicate the line that is causing that error for your edit to be useful, of course. – Jason C Oct 25 '13 at 22:28
  • You may also wish to take a look at http://sscce.org/. It's totally OK to be a "noob" at programming; that's why we're here. We only ask that you don't be a "noob" at asking questions (you'll get better answers anyways). – Jason C Oct 25 '13 at 22:38
  • Haha yea, still need to get use to the terms in programming to ask better questions. Will read it all up soon. Thanks so much. – Jay Oct 25 '13 at 22:48
1

Well that is because you defined the move method as a non-static method by saying public void to make it static you need to write public static void instead.

Also be aware that variable names are case sensitive, if you write Slide.move() you clearly call upon your Slide class because your variable was named slide.

vallentin
  • 23,478
  • 6
  • 59
  • 81