Re: Question: non-static method dummy(int) cannot be referenced from a static context

From: Adam Jenkins (adam_at_remove.thejenkins.me.org)
Date: 01/27/04


Date: Tue, 27 Jan 2004 15:45:38 -0500

Michael wrote:
> I've writen a rather extensive JApplet (a multiplayer chinese chess
> version). Works fine, but now I've run into a problem. Here's a small
> sample:
>
> public class test extends JApplet {
>
> public void init() {
> whatsoever.dummy(123);
> }
>
> public class whatsoever {
> public void dummy(int i) {}
> }
> }
>
> On compiling I get the error message: "test.java:13: non-static method
> dummy(int) cannot be referenced from a static context".
>
> This is strange, because I didn't declare ANYTHING static here, and I
> don't think JApplet or init() is static per default.

The problem is this statement:

   whatsoever.dummy(123);

Since "whatsoever" is a class name, this statement is interpreted by the
compiler as a call to the static method dummy(int) in the "whatsoever"
class. Since whatsoever.dummy(int) is *not* a static method, you get a
compile error. You either need to create an instance of "whatsoever"
and call dummy(int) on that instance, or make dummy(int) be a static
method.

Adam