• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

kathy sierra question doubt scjp 1.6

 
dilip kumarr
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Anyone please explain this code in detail ., i'm not able to understand ...
thanks in advance.

 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its very simple, you are calling a non-static method directly from a static method, this is not allowed. You need an instance of Frodo/Hobbit class to call the countGold method from the main method...
 
dipayan chatterjee
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi dilip

countGold(myGold, 6) basically resolves to this.countGold(myGold,6) and since the main method is static it means it is not running on any instance of class Frodo hence there exsists no object so cant invoke the instance method countGold(intx,inty) .

This is the basic reason why calling a non-static method directly from a static method, this is not allowed.


Hth
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic