• 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

Static Classwide?

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been studying and read that static is classwide, but does that mean the static method can be used anywhere within a particular class, or just within a block?..
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It means neither of the two things you mention. Access specifiers (public, protected, private) control where methods are visible. The keyword static means something different.

It means that the method "works on the whole class" instead of on one particular object of the class. For example, if you have a static member variable, there is only one instance of that variable for all objects of the class, as opposed to one instance for every object of the class.
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know this may sound stupid, but could you please list and example of when I would use static and when I would not use static?..

I've just been really confused since I've been studying, and this is a road block for me at the moment..
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This comes up all the time:-

  • A static method belongs to the class, not to any perticular object. (Also a static field, so I shall say "static member.")
  • [list]To call a static member bah from a class Foo you say Foo.bah or Foo.bah() rather than
  • There is only one of each static member in memory, whereas there is one object field for each instance, so there may be many different instance fields in memory.
  • Each object of the class has access to all its static members, but not vice versa. You cannot call an instance member from inside a static method, nor can you use the this keyword (and I think you can't use the super keyword in a statiuc method either).
  • Each instance has access to every static member, and it is always the same copy for all instances. So a change made to a static field by "myFoo.baah(234);" will change the static field for every other instance; it will have the same value if you call bah from myFoo.baaah(), yourFoo.baaah() or ourFoo.baaah().

  • When would you use a static method?
  • When you pass information and want an answer which does not affect the other fields. Example: the Math class has a sin() method. You pass a number and it works out the sine of that number (in radians). It doesn't affect any fields of the Math class. So it is declared as a static method. [In fact every public member of the Math class is static.]
  • When you want a field which is the same for every instance of the class. Example: you have a MyLinkedElement class which forms part of a MyLinkedList. Your add() method includes "count++;" and your remove() method includes "count--;"

  • The "count" field is a static field, which counts how many elements there are in the LinkedList. You have a public static getCount() method, and saying "MyLinkedElement.getCount()" tells you how many elements you have.[edit]Nearly forgot:
    Another use of static members is for "constants." If you have a constant, it will be the same always and the same for every member of the class. Many people declare such constants like this:-[/edit]
    CR
    [ August 25, 2006: Message edited by: Campbell Ritchie ]
     
    Ranch Hand
    Posts: 37
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    There's one more important point:
    As static methods are not instance methods, they can never be overridden.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic