Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Question: non-static method dummy(int) cannot be referenced from a static context

Reply
Thread Tools

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

 
 
Michael
Guest
Posts: n/a
 
      01-27-2004
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)
 
Reply With Quote
 
 
 
 
Christophe Vanfleteren
Guest
Posts: n/a
 
      01-27-2004
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".


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

--
Kind regards,
Christophe Vanfleteren
 
Reply With Quote
 
 
 
 
Markku Salminen
Guest
Posts: n/a
 
      01-27-2004
> 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 -

 
Reply With Quote
 
Adam Jenkins
Guest
Posts: n/a
 
      01-27-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: non-static method cannot be referenced from a static context Johannes Beekhuizen Java 1 08-05-2005 06:08 PM
Re: non-static method cannot be referenced from a static context Johannes Beekhuizen Java 0 08-05-2005 05:33 PM
non-static method cannot be referenced from a static context Johannes Beekhuizen Java 14 08-05-2005 04:48 PM
newbie Java question - non-static method cannot be referenced from a static context G. Burton Java 2 02-21-2004 07:45 PM
Please help: Non-static method wait(long) cannot be referenced from static context Gary Java 1 01-16-2004 11:35 PM



Advertisments