File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes still not clear on static? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "still not clear on static?" Watch "still not clear on static?" New topic
Author

still not clear on static?

Daniel .J.Hyslop
Ranch Hand

Joined: May 23, 2005
Posts: 55
Hello javaceese,

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?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
samdeep aarzoo
Ranch Hand

Joined: Jun 09, 2005
Posts: 160

Perhaps you're interested in how many of each breed there are


now please give how to write code for this :
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

I showed the "countByBreed()" method; you just need to do something like this in the Dog constructor:

samdeep aarzoo
Ranch Hand

Joined: Jun 09, 2005
Posts: 160
return ((Integer) breed.get(breedName)).intValue();

how u are using get method with String breed
there is no method get() for java.lang.String

can u please alter the code for return statement
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
It should probably be


Layne
[ July 16, 2005: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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).

Anyway, my point is "Bikash, lighten up!"
samdeep aarzoo
Ranch Hand

Joined: Jun 09, 2005
Posts: 160
Ernest
thanks for this great explanation
and i m extrenely sorry for that
i think i should make different thread and then asked the q
Daniel .J.Hyslop
Ranch Hand

Joined: May 23, 2005
Posts: 55
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: still not clear on static?
 
Similar Threads
Static Synchronized Methods
Synchronization
What's the reason ?
Is this thread safe?
difference between maven "compile" and "provided" scope