• 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

still not clear on static?

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I showed the "countByBreed()" method; you just need to do something like this in the Dog constructor:

 
samdeep aarzoo
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should probably be


Layne
[ July 16, 2005: Message edited by: Layne Lund ]
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic