aspose file tools*
The moose likes Beginning Java and the fly likes Problem in understanding static context... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem in understanding static context... " Watch "Problem in understanding static context... " New topic
Author

Problem in understanding static context...

Shiveen Pandita
Greenhorn

Joined: Jul 30, 2012
Posts: 25



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

Joined: Apr 06, 2010
Posts: 4490
    
    8

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

Joined: Dec 17, 2009
Posts: 133
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

Joined: Jul 30, 2012
Posts: 25

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

Joined: Jul 30, 2012
Posts: 25

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in understanding static context...