Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in understanding static context...

 
Shiveen Pandita
Greenhorn
Posts: 25
Java Windows
  • Mark post as helpful
  • send pies
  • 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!
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • 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).
 
Yogesh Gnanapraksam
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic