wood burning stoves 2.0*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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: 4240
    
    7

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: 4240
    
    7

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: declaring class members
 
Similar Threads
Access Modifiers pg 26 k&b book(doubt)
Simple Q, I'm newbie
Inheritance
Method is static, yet I get compiler error saying it cannot be accessed from static context