This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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: 4343
    
    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: 4343
    
    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
 
Don't get me started about those stupid light bulbs.
 
subject: declaring class members
 
Similar Threads
Method is static, yet I get compiler error saying it cannot be accessed from static context
Access Modifiers pg 26 k&b book(doubt)
Simple Q, I'm newbie
Inheritance