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

Discussion in 'Java' started by Michael, Jan 27, 2004.

  1. Michael

    Michael Guest

    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) :)
     
    Michael, Jan 27, 2004
    #1
    1. Advertising

  2. 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
     
    Christophe Vanfleteren, Jan 27, 2004
    #2
    1. Advertising

  3. Re: Question: non-static method dummy(int) cannot be referenced froma static context

    > 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 -
     
    Markku Salminen, Jan 27, 2004
    #3
  4. Michael

    Adam Jenkins Guest

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

    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
     
    Adam Jenkins, Jan 27, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gary
    Replies:
    1
    Views:
    4,015
    Ryan Stewart
    Jan 16, 2004
  2. G. Burton
    Replies:
    2
    Views:
    59,915
    Sudsy
    Feb 21, 2004
  3. Johannes Beekhuizen
    Replies:
    14
    Views:
    1,351
    John B. Matthews
    Aug 5, 2005
  4. Johannes Beekhuizen
    Replies:
    0
    Views:
    1,544
    Johannes Beekhuizen
    Aug 5, 2005
  5. Johannes Beekhuizen
    Replies:
    1
    Views:
    543
    Thomas Fritsch
    Aug 5, 2005
Loading...

Share This Page