• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method is static, yet I get compiler error saying it cannot be accessed from static context

 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21007
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The non-static variable being referred to here is the "this" variable.

Henry
 
mohammad shaid
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this & super cannot be used to invoke on nonstatic stuff(non-static methods fo eg)
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohammad shaid wrote:....static methods cannot be invoked on this as its nonstatic (like super)


Are you sure about this?

 
mohammad shaid
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ... ...
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course!

The following seems to work:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic