• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem in understanding static context...

 
Greenhorn
Posts: 25
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hey guys.. i know I'll be wasting your time with my stupid queries but I hope you'll guide me.

I came along this code in some book... it looks fine to me with the inheritance and all. but when i run it, it says you can't run a non static method from a static context. I'm stumped, because it nullifies my knowledge of Inheritance as the small class should inherit the big class and the count method along with it.

I'm stumped!
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Small class inherits the count() method, but because it's not a static method it belongs to instances of the class. So you need a Small object to be able to call the count() method on it. There are no instances created until you create one.

Just to show it's not a problem with the inheritance: you'll still have the same problem if you move the count() method to the Small class (try it out).
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are stumped because you are new to Object Oriented Programming.
You have written a class and to make use of the members of the class you have to create objects. Only then you can invoke the non-static members of the class.
This might be useful.

.


 
Shiveen Pandita
Greenhorn
Posts: 25
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:The Small class inherits the count() method, but because it's not a static method it belongs to instances of the class. So you need a Small object to be able to call the count() method on it. There are no instances created until you create one.

Just to show it's not a problem with the inheritance: you'll still have the same problem if you move the count() method to the Small class (try it out).



thanks.. I'm stupid.. I had read that just recently you know. It went out of my mind. I guess I need more practice.. I am preparing for OCJP and am from a Hardware background, so oops programming concepts are new to me.

anyways..appreciate the help
 
Shiveen Pandita
Greenhorn
Posts: 25
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yogesh Gnanapraksam wrote:You are stumped because you are new to Object Oriented Programming.



Yes I am

but still thanks for the link. appreciate the help mate.
 
A berm makes a great wind break. And we all like to break wind once in a while. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic