Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What is STATIC  RSS feed

 
Varun Rangarajan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks a lot Ernest....Will try and do those programs soon...
 
samir Sadiki
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Varun,
Here is a silly example that I hope will help you a little bit.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!