Article View: borland.public.jbuilder.java.swing
Article #1761Re: Getting out of static mode
From: John McGrath [TeamB]
Date: Wed, 16 Jul 2003 04:16
Date: Wed, 16 Jul 2003 04:16
82 lines
3653 bytes
3653 bytes
On 7/15/2003 at 3:40:14 PM, Craig Wilson wrote:
> "non-static method cannot be referenced from a static context."
Class (i.e. static) methods apply to the class as a whole, not to one
particular instance of the class.
Instance (non-static) methods apply to a single instance of the class.
These methods "know" which particular instance they apply to.
This is implemented by passing a hidden "this" parameter to each
instance method, so code in an instance method can reference "this",
either explicitly or implicitly.
Of course, this means that when you want to call an instance method,
you must pass it the "this" parameter. You can do this two ways: One
is to explicitly specify the instance, using something like
instance.method().
The other way is to have the compiler implicitly specify the "this"
parameter. This can only be done in an instance method for the same
class, since there is no "this" value available in a static method and
the "this" value in methods of other classes is not the right type.
The following code should make things more clear:
class Foo {
int vi1 = 1;
int i1() { // Has a hidden "this" parameter.
// The following references this.vi1, using the hidden "this".
System.out.println( vi1 );
return 1;
}
int i2() { // Also has a hidden "this" parameter
// The following references this.i1(), using hidden "this".
i1();
return 2;
}
static int c1() { // static - No hidden "this" parameter.
// OK. The c2() method is static, so it can be called from
// another static method. No "this" value is needed.
c2();
return 3;
}
static int c2() { // static - No hidden "this" parameter.
// The following is Invalid, since there is no "this" value
// to pass to i1().
i1();
return 4;
}
static int c3() { // static - No hidden "this" parameter.
Foo foo = new Foo();
// Here, "foo" is passed as the "this" parameter.
foo.i1();
return 5;
}
// The following is OK. "vi2" is an instance variable, so it
// is part of an instance of Foo. That instance is passed to
// the i2() method.
int vi2 = i2();
// This is OK. "vc1" is a class variable, so it is not part
// of an instance of Foo. But c2() does not need an instance
// so there is no problem.
static int vc1 = c2();
// This is invalid. "vc2" is a class variable, so it is not
// part of an instance of Foo. i1() requires a "this" value
// and there is nothing to pass it.
static int vc2 = i1();
}
--
Regards,
John McGrath [TeamB]
Technical Details
Message-ID:
<3f14d1b6$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-01!sn-xit-09!supernews.com!newsfeed.news2me.com!newsfeed.media.kyoto-u.ac.jp!newsfeed01.tsnz.net!news.xtra.co.nz!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<3f145858@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,5097939110998c9d X-Google-Attributes: gid111d45,public X-Google-ArrivalTime: 2003-07-15 21:36:18 PST From: "John McGrath [TeamB]" <ng-b@jpmcgrath.net> Subject: Re: Getting out of static mode Newsgroups: borland.public.jbuilder.java.swing References: <3f145858@newsgroups.borland.com> User-Agent: XanaNews/1.15.3.1 MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 NNTP-Posting-Host: 151.197.210.86 Message-ID: <3f14d1b6$1@newsgroups.borland.com> Date: 15 Jul 2003 21:16:54 -0700 X-Trace: newsgroups.borland.com 1058329014 151.197.210.86 (15 Jul 2003 21:16:54 -0700) Lines: 82 X-Authenticated-User: jpmcgrath Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-01!sn-xit-09!supernews.com!newsfeed.news2me.com!newsfeed.media.kyoto-u.ac.jp!newsfeed01.tsnz.net!news.xtra.co.nz!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail