• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A spurious code ???

 
vamsi acharya
Ranch Hand
Posts: 55
Android Chrome Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The above code looks completely alright to me but seems like an error rises at line 4 ...can anyone explain me why ??
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
vamsi acharya
Ranch Hand
Posts: 55
Android Chrome Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
Eclipse IDE Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 55
Android Chrome Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 144
Java Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic