Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #13075
| From | Lew <noone@lewscanon.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Beginners Problem - Static reference to non-static method |
| Date | 2012-03-20 06:34 -0700 |
| Organization | albasani.net |
| Message-ID | <jka10r$p2h$1@news.albasani.net> (permalink) |
| References | <jk9vfl$gfl$1@theodyn.ncf.ca> |
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
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web