aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A spurious code ??? 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 "A spurious code ???" Watch "A spurious code ???" New topic
Author

A spurious code ???

vamsi acharya
Ranch Hand

Joined: Jun 28, 2009
Posts: 55



The above code looks completely alright to me but seems like an error rises at line 4 ...can anyone explain me why ??


"Known is a drop , unknown is an ocean " !
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18990
    
  40

vamsi acharya wrote:
The above code looks completely alright to me but seems like an error rises at line 4


Does the error message reported by the compiler give you a hint on what it could be?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
vamsi acharya
Ranch Hand

Joined: Jun 28, 2009
Posts: 55

90
Henry Wong wrote:
vamsi acharya wrote:
The above code looks completely alright to me but seems like an error rises at line 4


Does the error message reported by the compiler give you a hint on what it could be?

Henry


I found this code in a certification book and the answer tells you that " The Short myGold is autoboxed correctly , but the countGold() method cannot be invoked from a static context " ....I really cant understand whats with the static context here !!
Aashu Mahajan
Ranch Hand

Joined: May 27, 2011
Posts: 114



The above code looks completely alright to me but seems like an error rises at line 4 ...can anyone explain me why ??



Hi, vamsi acharya
You should first notice that 'countGold(int x, int y )' is non-static method in Hobbit class and you are calling a non-static method from static context without any reference, it is invalid and causes Compile Time Error
I have correct the code below :




vamsi acharya
Ranch Hand

Joined: Jun 28, 2009
Posts: 55



Hi, vamsi acharya
You should first notice that 'countGold(int x, int y )' is non-static method in Hobbit class and you are calling a non-static method from static context without any reference, it is invalid and causes Compile Time Error





Is it possible to access the method through 'this' keyword ??? ...Why do we have to create an instance for the method of same class ??
Hama Kamal
Ranch Hand

Joined: May 29, 2011
Posts: 144

vamsi acharya wrote:

The above code looks completely alright to me but seems like an error rises at line 4 ...can anyone explain me why ??


Hello Vamsi !

you can not call a non static method in that way ,,, there are two solutions for your problem
1-make the method static as shown below



2-create an object for Hobbit class and then call the non static method through the object as shown below




``Worry does not empty tomorrow of its sorrow; it empties today of its strength.''
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A spurious code ???