Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Beginners Problem - Static reference to non-static method

Reply
Thread Tools

Beginners Problem - Static reference to non-static method

 
 
William Colls
Guest
Posts: n/a
 
      03-20-2012
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.


 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      03-20-2012
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/wikipedi.../c/cf/Friz.jpg
 
Reply With Quote
 
 
 
 
William Colls
Guest
Posts: n/a
 
      03-20-2012
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.
 
Reply With Quote
 
Gordon Levi
Guest
Posts: n/a
 
      03-20-2012
William Colls <(E-Mail Removed)> 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();
}
}



 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-20-2012
On Tue, 20 Mar 2012 09:07:50 -0400, William Colls
<(E-Mail Removed)> 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.
 
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
static reference in a non-static method TheTravellingSalesman Java 5 08-30-2007 01:16 AM
Beginners Question Regarding Static Web Pages Roger stevenson ASP .Net 8 07-01-2005 04:05 AM
Question: non-static method dummy(int) cannot be referenced from a static context Michael Java 3 01-27-2004 08:45 PM
Please help: Non-static method wait(long) cannot be referenced from static context Gary Java 1 01-16-2004 11:35 PM
Why do static and non-static method names collide? =?ISO-8859-1?Q?Thomas_Gagn=E9?= Java 12 07-05-2003 04:59 PM



Advertisments