Beginners Problem - Static reference to non-static method

Discussion in 'Java' started by William Colls, Mar 20, 2012.

  1. I am novice with Eclipse, and a beginner with Java, but I have 20+ years
    experience as a programmer - coming to OO late in life.

    I Have some code that looks like this:

    // begin java

    package com.mine.package

    class MyClass {

    import java.io.*

    public static void main() {

    try {
    runMyMethod(); // <---- error here
    }
    catch (Exception e) {
    System.out.println(e.getMessage());
    }
    }

    public void runMyMethod() throws Exception {
    // do some things
    }
    }

    // end java

    However, Eclipse generates the error "Cannot make static reference to to
    non-static method in type MyClass" at the point noted.

    I understand what it means (I think), but I don't understand why it is a
    problem, and more importantly - How to fix it. There is a whole bunch
    more stuff in the class. Making runMyMethod static fixes the problem
    here, but then everything else in the class also has to be static.

    Eventually, the class MyClass will be integrated into a much larger
    project, and the local main method goes away. I tried creating a
    seperate class with the main method, and importing MyClass. However,
    Eclipse seemed to be unable to resolve the runMyMethod from the imported
    class. But maybe I wasn't doing it properly.

    If anyone can offer guidance and/or suggestions as how best to proceed,
    I will be most greatful.

    Thanks all for your time.

    William.
     
    William Colls, Mar 20, 2012
    #1
    1. Advertising

  2. William Colls

    Lew Guest

    William Colls wrote:
    > I am novice with Eclipse, and a beginner with Java, but I have 20+ years
    > experience as a programmer - coming to OO late in life.
    >
    > I Have some code that looks like this:
    >
    > // begin java
    >
    > package com.mine.package
    >
    > class MyClass {


    This had better be 'public', yes?

    > import java.io.*


    The 'import' directives must follow the 'package' directive and precede the
    class definition.

    > public static void main() {
    >
    > try {
    > runMyMethod(); // <---- error here
    > }
    > catch (Exception e) {
    > System.out.println(e.getMessage());
    > }
    > }
    >
    > public void runMyMethod() throws Exception {
    > // do some things
    > }
    > }
    >
    > // end java
    >
    > However, Eclipse generates the error "Cannot make static reference to to
    > non-static method in type MyClass" at the point noted.


    There are two kinds of members in a class, class members where there's one of
    the member shared by every class instance, and instance members, where each
    instance has its own. Members declared with the 'static' keyword belong to the
    class, and do not need an instance to own them. Other members belong to an
    instance, and there must be an instance created first in order to access them.

    'main()' is a class ('static') member, 'runMyMethod()' is an instance member.

    In order for a class member, or an instance of a different class, to access
    the instance member, it must do so through an instance, e.g.,

    MyClass instanc = new MyClass();
    instanc.runMyMethod();

    > I understand what it means (I think), but I don't understand why it is a
    > problem, and more importantly - How to fix it. There is a whole bunch more
    > stuff in the class. Making runMyMethod static fixes the problem here, but then
    > everything else in the class also has to be static.


    Create an instance.

    > Eventually, the class MyClass will be integrated into a much larger project,
    > and the local main method goes away. I tried creating a seperate class with
    > the main method, and importing MyClass. However, Eclipse seemed to be unable
    > to resolve the runMyMethod from the imported class. But maybe I wasn't doing
    > it properly.


    You need an instance.

    --
    Lew
    Honi soit qui mal y pense.
    http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
     
    Lew, Mar 20, 2012
    #2
    1. Advertising

  3. On 03/20/2012 09:07 AM, William Colls wrote:
    > I am novice with Eclipse, and a beginner with Java, but I have 20+ years
    > experience as a programmer - coming to OO late in life.
    >
    > I Have some code that looks like this:
    >
    > // begin java
    >
    > package com.mine.package
    >
    > class MyClass {
    >
    > import java.io.*
    >
    > public static void main() {
    >
    > try {
    > runMyMethod(); // <---- error here
    > }
    > catch (Exception e) {
    > System.out.println(e.getMessage());
    > }
    > }
    >
    > public void runMyMethod() throws Exception {
    > // do some things
    > }
    > }
    >
    > // end java
    >
    > However, Eclipse generates the error "Cannot make static reference to to
    > non-static method in type MyClass" at the point noted.
    >
    > I understand what it means (I think), but I don't understand why it is a
    > problem, and more importantly - How to fix it. There is a whole bunch
    > more stuff in the class. Making runMyMethod static fixes the problem
    > here, but then everything else in the class also has to be static.
    >
    > Eventually, the class MyClass will be integrated into a much larger
    > project, and the local main method goes away. I tried creating a
    > seperate class with the main method, and importing MyClass. However,
    > Eclipse seemed to be unable to resolve the runMyMethod from the imported
    > class. But maybe I wasn't doing it properly.
    >
    > If anyone can offer guidance and/or suggestions as how best to proceed,
    > I will be most greatful.
    >
    > Thanks all for your time.
    >
    > William.
    >
    >

    Thanks to Patricia and Lew - so simple once you understand.
     
    William Colls, Mar 20, 2012
    #3
  4. William Colls

    Gordon Levi Guest

    William Colls <> wrote:

    [snip]

    I had the same problem when I started with Java and I received a lucid
    explanation from "Jacob" in this group. I failed to find the posts on
    Google so I have reproduced them below.

    I wrote:

    > I keep writing code which produces the error message "non-static
    > method foo() cannot be referenced from a static context". Although I
    > can usually "fix" the error I am sure that I do not have a proper
    > understanding of the problem. I would be grateful if there is someone
    > who recognises my confusion and can point me to an explanation which
    > resolves it.


    And Jacob replied:

    To call a non-static method (myMethod) you need
    an instance of the class to access it through:

    instance.myMethod(); or
    this.myMethod();

    The latter case is when you're within an instance
    already (i.e. within a non-static method of the
    same class) and as "this." is implied, you normally
    just write "myMethod()".

    In your case you are within a static method (where
    there is no "this") so you cannot call just
    "myMethod()", i.e. "this.myMethod()".

    You have three options:

    o Create an instance and call instance.myMethod()
    o Make myMethod static
    o Make the method you call it from non-static

    The problem you refer to is common when dealing
    with main() which is static:


    public class HelloWorld
    {
    public void printHello()
    {
    System.out.println ("Hello World");
    }

    public static void main (String args[])
    {
    printHello(); // Error

    // Correct:
    HelloWorld helloWorld = new HelloWorld();
    helloWorld.printHello();
    }
    }
     
    Gordon Levi, Mar 20, 2012
    #4
  5. William Colls

    Roedy Green Guest

    On Tue, 20 Mar 2012 09:07:50 -0400, William Colls
    <> wrote, quoted or indirectly quoted someone
    who said :

    >
    >However, Eclipse generates the error "Cannot make static reference to to
    >non-static method in type MyClass" at the point noted.


    Look that error up on page
    http://mindprod.com/jgloss/compileerrormessages.html

    I give lists of possible causes and sometimes what you have to do to
    fix it.


    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    It is almost impossible to keep things in synch manually. Instead:
    -Keep each fact in only one central database (not necessarily SQL),
    and access it as needed. Since there is only one copy of each fact,
    there is nothing to get out of synch.
    -Use some automated tool so that if you change a fact is one place,
    it automatically updates the others.
    -Write a sanity checker you run periodically to ensure all is consistent.
    This is the strategy compilers use.
    -Document the procedures needed to keep all in synch if you change
    something and rigidly and mechanically follow them.
     
    Roedy Green, Mar 20, 2012
    #5
    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. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    Why do static and non-static method names collide?

    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 2, 2003, in forum: Java
    Replies:
    12
    Views:
    5,647
    cgbusch
    Jul 5, 2003
  2. Gary
    Replies:
    1
    Views:
    3,991
    Ryan Stewart
    Jan 16, 2004
  3. Michael
    Replies:
    3
    Views:
    4,942
    Adam Jenkins
    Jan 27, 2004
  4. TheTravellingSalesman

    static reference in a non-static method

    TheTravellingSalesman, Aug 29, 2007, in forum: Java
    Replies:
    5
    Views:
    6,436
    TheTravellingSalesman
    Aug 30, 2007
  5. rickman
    Replies:
    5
    Views:
    428
    rickman
    Mar 30, 2013
Loading...

Share This Page