Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HELP NEEDED FOR CALLING METHOD OF THE SAME CLASS

 
Yasser Shaikh
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Outputs:
displaying shape
displaying shape

this is what i found in my scjp book while studying ...
but when i was trying to run this piece of code on my pc i got an error...
which i then corrected by just creating an object of the TestShape class
and calling doShapes method with that object...
please guide me!
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is it you do not understand?

The code from K&B works as it is written in the book (SCJP6, Chapter 2, Inheritance, Is-A, Has-A, page 91), I get no exceptions, just he output as described. What exception did you get?

I can't see why you need an instance of TestShape - it's static methods you are calling and they do not require an object instance, they can be called on the class instance.
 
Yasser Shaikh
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No... i dont get exceptions i got two errors they are listed below:

C:\Documents and Settings\RNS\My Documents\TestShape.java:35: non-static method doShape(GameShape) cannot be referenced from a static context
doShape(player);
^
C:\Documents and Settings\RNS\My Documents\TestShape.java:36: non-static method doShape(GameShape) cannot be referenced from a static context
doShape(tile);
^
2 errors

and didnt get what you are trying to say...
its not the static method that i am callin ....
doShapes() is a non-static method...
please reply ASAP!!
 
Maneesh Godbole
Saloon Keeper
Posts: 11092
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yasser - the code you presented here works perfectly and "doShapes" is static, but in your program you get an error. What does that tell you?

Your program has at least two mistakes in it.

1) There should be no "doShape" method, but should be a "doShapes" method.
2) Carefully check your declaration of the "doShapes" method. I think you'll find something missing. Clue: it begins with "s".



 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't even see any code that could generate that error.
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the code they have posted, no.

In the code they typed into their source file? Different question.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic