File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help on this SCJP question

 
Ahsan Parvez
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am preparing for the SCJP exam. Can someone help me with the answer of this problem? What does it mean when it says the countGold() method cannot be invoked from a static context?



 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


please post the question inside code tag
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
every instance variable or method is considered to be the property of an object of that class.

here countGold method will be a property of an object not that of the class hence an object is indispensable to invoke it.

since static methods does not have a this (as they are the property of the class not of any object)
hence we can't call or invoke a non-static method from static one . also we can't use any non-static variable too from a static method .

hence we must need an object to invoke the method or to use an instance variable.



avi sinha
 
Ahsan Parvez
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot for the help Sir.
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's okay . it should be done to increase the readability of the code.

avi sinha
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one more thing please quote the source of the question too . & welcome to JavaRanch

avi sinha
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic