Beginners Problem - Static reference to non-static method


W

William Colls

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

Advertisements

L

Lew

William said:
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.
 
W

William Colls

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

Gordon Levi

[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 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();
}
}
 
Ad

Advertisements

R

Roedy Green

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.
 

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

Top