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

declaring class members

Anjali Vaidya
Ranch Hand

Joined: Jan 25, 2011
Posts: 40



Please tell me why is m.coolMethod giving an error?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4464
    
    8

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

Joined: Jan 25, 2011
Posts: 40

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

Joined: Apr 06, 2010
Posts: 4464
    
    8

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

Joined: Jan 25, 2011
Posts: 40

I extended the zoo class and printed using this.coolMethod()
it worked. Thank you
 
 
subject: declaring class members