I may sound simple minded in this explanation of my problem , but I am having a static nightmare.I am still finding it somewhat difficult to understand the concept of the static modifier in the context of calling methods.I Understand the class variables are shared between all objects ie they remain constant for each individual object.The translation of that explanation extends to the static methods. I am finding it difficult to understand when calling methods that are declared static ,what benefits are produced by doing this?; what are the differences between calling static methods and non -static methods?;what differentiates the need to call static- non static methods? I have read quite a lot on this subject,but nothing seems to explain it with clear insight.Any help would be appreciated. Thanks
an island in the sun <br />with a language of many tongue?
Imagine you have a class Dog. Each instance of Dog represents a Dog.
class Dog {}
Every dog has some unique characteristics: at a minimum, age and breed. Let's have our class reflect that:
Those are instance variables -- every Dog object gets its own copies of them. We might want to have a method that returns a Dog's age in "dog years", which is computable from the age in years. This must be an instance method so it can access the instance variable age:
Now, another thing you might want to know is how many Dogs there are in the world. That's not a property of a single dog -- it's a property of the class of Dog: i.e., it should be kept in a static variable:
Perhaps you're interested in how many of each breed there are. Well, that's easy enough. We need a static variable to hold a table of breed/count pairs. When a Dog is created, we'd need to "register" it, and then later we can ask about individual breeds. Because this method uses only static data, it can (and should) be a static method. Then we don't need a Dog object to ask how many Collies there are: we just ask the class:
And that's why we need static methods: to take some action "associated" with a class, but not with any individual object of that class.
The point of this exercise was not to produce an industrial-strength dog-breed-counting facility, but to explain what static methods are for. It's really quite hard to produce example snippets like this that compile on the first try (I should point out that Layne's correction has a typo in it, too -- the variable was named breedCounts.) When I'm writing a book, I make sure all the code samples compile. When I'm answering questions on JavaRanch, I (and I assume most people) skip that step, so the occasional typo sneaks though. I think it's a very good policy that if an example is intended to illustrate concept A (static methods, here), you look at it with respect to that concept, only, and don't bother trying to probe its relationship to concept B (mapping string to numbers).
hi ernest, Sorry for the delay in confirmation of your reply by the original postee.That was a very good example of static uses , no hyped up jargon that beginners cannot understand, and I might add all replies I have had within javaversity have been in the same vein.The Dog class has cleared up quite a lot of my questions. Putting it simply static methods look at the bigger picture of what is happening in conjunction with it`s own class.Still one thing confuses me .Taking the Thread class as an example it has a static method "sleep(10)" you call sleep(10) by Thread.sleep(10)which is telling the current thread running to sleep for 10m`secs. This ,I take it ,singles out each thread object individually when called ,therefore this method has to be static as it identifies each individual object being called rather than working on a particular reference,the static modifier has allowed the sleep(10) method to be called without an actual reference initialisation to an object.The currentThread() method works on the same principles it identifies a thread object with no reference attached,it is just interested in the objects.Is this assumption correct or am I barking up the wrong tree.
an island in the sun <br />with a language of many tongue?