| Author |
Method is static, yet I get compiler error saying it cannot be accessed from static context
|
Sandra Bachan
Ranch Hand
Joined: Feb 18, 2010
Posts: 434
|
|
Sierra/Bates chapter 1 code:
I changed it to the following:
I get compiler error:
Moo.java:10: non-static variable this cannot be referenced from a static context
System.out.println("Moo says, " + this.coolMethod());
^
1 error
But I am not referencing a variable...please explain
|
Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16815
|
|
The non-static variable being referred to here is the "this" variable.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
mohammad shaid
Ranch Hand
Joined: May 05, 2010
Posts: 86
|
|
|
this & super cannot be used to invoke on nonstatic stuff(non-static methods fo eg)
|
Thanks & Regards,
shaad
|
 |
pete stein
Bartender
Joined: Feb 23, 2007
Posts: 1561
|
|
mohammad shaid wrote:this & super cannot be used to invoke on nonstatic stuff(non-static methods fo eg)
Please clarify what you mean here as the statement above confuses me. Thanks.
|
 |
mohammad shaid
Ranch Hand
Joined: May 05, 2010
Posts: 86
|
|
|
coolMethod in the above program is static and static methods cannot be invoked on this as its nonstatic (like super).. try create two classes(one inheriting another) and create static methods in both classes with same name.. try call the overridden method using super from the overriding method.. you will get to know..
|
 |
pete stein
Bartender
Joined: Feb 23, 2007
Posts: 1561
|
|
mohammad shaid wrote:....static methods cannot be invoked on this as its nonstatic (like super)
Are you sure about this?
|
 |
mohammad shaid
Ranch Hand
Joined: May 05, 2010
Posts: 86
|
|
|
ya well.. thanks for correcting me.. i took it wrong.. thought we cannot invoke static methods on this... but this is non static and it cannot be invoked from static method.. am i right???
|
 |
pete stein
Bartender
Joined: Feb 23, 2007
Posts: 1561
|
|
mohammad shaid wrote:ya well.. thanks for correcting me.. i took it wrong.. thought we cannot invoke static methods on this... but this is non static and it cannot be invoked from static method.. am i right???
Yes. As far as I understand it, the main problem for the OP is that they tried to use "this" from within a static method, and inside a static method, "this" simply doesn't exist.
|
 |
Sandra Bachan
Ranch Hand
Joined: Feb 18, 2010
Posts: 434
|
|
Now I understand that this() and super() are non-static.
According to the text, and invocation for this() from a static context is supposed to work:
Please clarify "The preceding line works because Moo can inherit the public method"
|
 |
Jim Hoglund
Ranch Hand
Joined: Jan 09, 2008
Posts: 525
|
|
Pete : In your "are-you-sure" code, you note that the compiler complains, but the code still
runs okay. Consider the situation where instance 'aa' is of type AA. Its static methods can be
called in two ways: AA.staticMethod() -or- aa.staticMethod(). Your example behaves the same
way. Reference 'this' has a known type that can be examined for the requested static method.
Jim ... ...
|
BEE MBA PMP SCJP-6
|
 |
Jim Hoglund
Ranch Hand
Joined: Jan 09, 2008
Posts: 525
|
|
Sandra : Your code appears again below with the 'z' object removed, because (unless it's
marked private) coolMethod() can be called directly from anywhere in Moo; it is a member
of Moo. As you demonstrated, creating a Moo object does no harm but the instance is not
needed to access coolMethod(). Jim ... ...
|
 |
Sandra Bachan
Ranch Hand
Joined: Feb 18, 2010
Posts: 434
|
|
Hi Jim,
Now I understand.
I do have a follow-on question. When I run the code as is (without adding public static void main()), I get the following error:
Exception in thread "main" java.lang.NoSuchMethodError: main
How to fix this?
|
 |
pete stein
Bartender
Joined: Feb 23, 2007
Posts: 1561
|
|
Sandra Bachan wrote:
I do have a follow-on question. When I run the code as is (without adding public static void main()), I get the following error:
Exception in thread "main" java.lang.NoSuchMethodError: main
How to fix this?
Hm, perhaps add a main method? ;)
|
 |
Sandra Bachan
Ranch Hand
Joined: Feb 18, 2010
Posts: 434
|
|
Of course!
The following seems to work:
|
 |
 |
|
|
subject: Method is static, yet I get compiler error saying it cannot be accessed from static context
|
|
|