Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #13072 > unrolled thread
| Started by | William Colls <william.colls@rogers.com> |
|---|---|
| First post | 2012-03-20 09:07 -0400 |
| Last post | 2012-03-20 11:28 -0700 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.java.programmer
Beginners Problem - Static reference to non-static method William Colls <william.colls@rogers.com> - 2012-03-20 09:07 -0400
Re: Beginners Problem - Static reference to non-static method Patricia Shanahan <pats@acm.org> - 2012-03-20 06:13 -0700
Re: Beginners Problem - Static reference to non-static method Lew <noone@lewscanon.com> - 2012-03-20 06:34 -0700
Re: Beginners Problem - Static reference to non-static method William Colls <william.colls@rogers.com> - 2012-03-20 10:21 -0400
Re: Beginners Problem - Static reference to non-static method Gordon Levi <gordon@address.invalid> - 2012-03-21 02:15 +1100
Re: Beginners Problem - Static reference to non-static method Roedy Green <see_website@mindprod.com.invalid> - 2012-03-20 11:28 -0700
| From | William Colls <william.colls@rogers.com> |
|---|---|
| Date | 2012-03-20 09:07 -0400 |
| Subject | Beginners Problem - Static reference to non-static method |
| Message-ID | <jk9vfl$gfl$1@theodyn.ncf.ca> |
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.
[toc] | [next] | [standalone]
| From | Patricia Shanahan <pats@acm.org> |
|---|---|
| Date | 2012-03-20 06:13 -0700 |
| Message-ID | <R9WdnfwTEIAOH_XSnZ2dnUVZ_oqdnZ2d@earthlink.com> |
| In reply to | #13072 |
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.
You have two options at this point:
1. Make all those methods static.
2. Create an object, and reference its methods:
new MyClass().runMyMethod();
Patricia
[toc] | [prev] | [next] | [standalone]
| From | Lew <noone@lewscanon.com> |
|---|---|
| Date | 2012-03-20 06:34 -0700 |
| Message-ID | <jka10r$p2h$1@news.albasani.net> |
| In reply to | #13072 |
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
[toc] | [prev] | [next] | [standalone]
| From | William Colls <william.colls@rogers.com> |
|---|---|
| Date | 2012-03-20 10:21 -0400 |
| Message-ID | <jka3p1$l8f$1@theodyn.ncf.ca> |
| In reply to | #13072 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Gordon Levi <gordon@address.invalid> |
|---|---|
| Date | 2012-03-21 02:15 +1100 |
| Message-ID | <sl7hm7dpl2b44pr4ca063u4bs2q63o1e6o@4ax.com> |
| In reply to | #13072 |
William Colls <william.colls@rogers.com> 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();
}
}
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2012-03-20 11:28 -0700 |
| Message-ID | <31jhm7p8nralld5h9qabl2djlfj51igu0m@4ax.com> |
| In reply to | #13072 |
On Tue, 20 Mar 2012 09:07:50 -0400, William Colls <william.colls@rogers.com> 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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web