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. Advertisements

  2. William Colls

    Lew Guest

    This had better be 'public', yes?
    The 'import' directives must follow the 'package' directive and precede the
    class definition.
    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();
    Create an instance.
    You need an instance.
     
    Lew, Mar 20, 2012
    #2
    1. Advertisements

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

    Gordon Levi Guest

    [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.
    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

    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. Advertisements

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 (here). After that, you can post your question and our members will help you out.