File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

declaring class members

 
Anjali Vaidya
Ranch Hand
Posts: 40
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Please tell me why is m.coolMethod giving an error?
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a clue: on the line that is giving an error, what does 'm' refer to? Where is it declared, and what is its scope?
 
Anjali Vaidya
Ranch Hand
Posts: 40
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay. i got it. because m is a local variable which only works in the main method
then how do i make an instance of Moo class call Zoo? is inheriting Zoo the only way?
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly do you mean by "call Zoo"?

On line 10 you're successfully calling the coolMethod method of Zoo, via an instance of Zoo, which is z. If you want to call the coolMethod method on your instance of Moo, first you need to define it in Moo somehow. Yes, inheriting from Zoo would be one way. Just adding the method to Moo would be another - which is better depends on why you're doing it.

Once you've done that, you can call it on the current instance on line 11 by using this.coolMethod(), or just coolMethod().
 
Anjali Vaidya
Ranch Hand
Posts: 40
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I extended the zoo class and printed using this.coolMethod()
it worked. Thank you
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic