• 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

What is STATIC

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i know it is meant to acess the main() method without an object...but why didnt they make the main() method alone an exception and make the language so that an object is not required to call main() ? why create something called "static" in the first place?
 
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 "static" keyword is useful any time you want a method or value associated with a class but not an object. One trivial example is all the methods in the java.lang.Math class that compute square roots, absolute values, etc. These are really mathematical functions in the pure sense of the word; they don't need an object to operate on -- so they're all static, and you just call them using the class name: Math.sqrt(), for example.

A slightly more interesting example: suppose you were writing a class and one of your requirements was that you needed to keep track of how many objects were created from that class while the program was running. Where should that count go? It can go in any object of that class, since then every object would have a different copy of it. It can't be a local variable in main() or anywhere else, since objects might be created anywhere. The answer is to put that count in a static variable in the class. This variable belongs to the class, but one copy is shared by all objects, rather than each object having its own. Make sense?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the reason for "static" was simply to make main an access point, then I don't think it would have made its way into the language. But main aside, static is an extremely valuable feature.

As an example, consider the Math class. All of the methods and constants are static. There is never a reason to create a Math object -- in fact, you can't (because it's a final class with a private constructor).

Or consider a static variable, which is by definition shared by all objects of the class.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmmm... if you go to the on-line dictionary site http://onelook.com
and enter "static", its first "quick definition: is:

angry criticism (Example: "They will probably give you a lot of static about your editorial")

Should we take this as a sign not to overuse the static keyword
 
Varun Rangarajan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quote
"It can go in any object of that class, since then every object would have a different copy of it. It can't be a local variable in main() or anywhere else, since objects might be created anywhere. The answer is to put that count in a static variable in the class. This variable belongs to the class, but one copy is shared by all objects, rather than each object having its own. Make sense?"

---------------------------------------------------------------------------

Sorry! Did not understand this...
("It can go in any object of that class, since then every object would have a different copy of it.) - diff copy of what? the object count? Did you mean it CAN go or it CANT go? If it CANT go, whats the problem if every object had its own copy of the count?

(It can't be a local variable in main() or anywhere else, since objects might be created anywhere. ) - dont get this either ... guess iam the greenest horn around.

(The answer is to put that count in a static variable in the class. This variable belongs to the class, but one copy is shared by all objects, rather than each object having its own. Make sense?") - Overall I was confused...I would be grateful if you could explain in detail...Thanks
 
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
Yes, that's a typo, it should have been "can't." If every object had its own separate "count" variable, how would you update them all? You couldn't, but even if you could, that's potentially a lot of wasted storage and a lot of wasted time, yes? Why store a million copies of something rather than just storing one? Why update a million variables instead of just one?

The comment about local variables in main() won't make sense to you until you've written a program that's somewhat larger than the typical single-class student program. "Real" Java programs often have hundreds or even thousands of classes, and potentially any method in any of those classes might want to create an object of this class we're talking about. None of those methods in any of those classes have access to local variables declared in the main() method, but they could access a static variable in the class (or the class's constructor could access it for them.)

I'm afraid I can't do much better on explaining why "static" is needed until you've written a few non-trivial programs that involve at least three or four classes; at that point come back and read what I've written over again and it should make more sense.
 
Varun Rangarajan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Ernest....Will try and do those programs soon...
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Varun,
Here is a silly example that I hope will help you a little bit.
 
Get meta with me! What pursues us is our own obsessions! But not this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic