File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help on this SCJP question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help on this SCJP question" Watch "Help on this SCJP question" New topic
Author

Help on this SCJP question

Ahsan Parvez
Greenhorn

Joined: Oct 19, 2009
Posts: 2
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

Joined: Mar 15, 2009
Posts: 452



please post the question inside code tag


SCJP 5.0 SCWCD 5.0
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 452

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

Joined: Oct 19, 2009
Posts: 2
Thanks alot for the help Sir.
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 452

it's okay . it should be done to increase the readability of the code.

avi sinha
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 452

one more thing please quote the source of the question too . & welcome to JavaRanch

avi sinha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help on this SCJP question