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

M

Michael

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. Now I can get
around with it by declaring the entire class whatsoever as static, but
as I already said, my JApplet is already very big, and I don't think
it's very clever to rename every method in my whatsoever class to
static, along with all variables used there :-/

Does anyone know an easy solution to this problem, or can at least
give me an explanation why not?

mfg Michael

PS: If this is in the wrong newsgroup, please excuse, I'm quite new
around here, and my english is not the best (to put it mildly) :)
 
C

Christophe Vanfleteren

Michael said:
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".

You're trying to run the instance method dummy without having an instance of
whatever.

Either you need to make dummy static, or you need to create an instance of
whatsoever, and call dummy on that.
This is strange, because I didn't declare ANYTHING static here, and I
don't think JApplet or init() is static per default. Now I can get
around with it by declaring the entire class whatsoever as static, but
as I already said, my JApplet is already very big, and I don't think
it's very clever to rename every method in my whatsoever class to
static, along with all variables used there :-/
You'd better go to the tutorial (http://java.sun.com/docs/books/tutorial/),
and catch up on the part on static variables/methods/classes. You seem kind
of confused here :)
Does anyone know an easy solution to this problem, or can at least
give me an explanation why not?

mfg Michael

PS: If this is in the wrong newsgroup, please excuse, I'm quite new
around here, and my english is not the best (to put it mildly) :)

I'm sure your English is much better than the German of many of us :)
 
M

Markku Salminen

public class test extends JApplet {
public void init() {
whatsoever.dummy(123);
}

public class whatsoever {
public void dummy(int i) {}
}
}

First, is there a good reason to put a class inside of another?

On compiling I get the error message: "test.java:13: non-static method
dummy(int) cannot be referenced from a static context".

You haven't made an instance of "whatsoever". Without
an instance you can call only static methods and
"dummy" isn't one.

PS: If this is in the wrong newsgroup, please excuse, I'm quite new

This place is perfect :)


- Markku -
 
A

Adam Jenkins

Michael said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,979
Messages
2,570,184
Members
46,721
Latest member
DollieSawt

Latest Threads

Top