Monday, 26 September 2011

Java: Static Variables and Methods

    We know about instance variables and methods. These are the variables and methods that belong to an object, a particular instance of some class. On top of this, Java supports the use of variables and methods that belong to the class as a whole. These are the static ones.

    This class sure is getting used a lot. Here, I've declared a static variable as a counter. Since this belongs to the class and not any particular instance, incrementing it in the constructor increments the count on each new object I create.

CLICK ME SO YOU CAN READ ME!

Probably could have shown the effect with fewer items, but still. You can see that for each new object we create, the counter increases by 1 on each item. Fun times. Good for keeping track. If count wasn't static, on each object initialization, it would be 0, and you'd get a whole bunch of 1's, which is fairly useless. The general point is, static variables are accessible by any instance of an object, and it changes it for all other instances as well.

    You can have static methods, too, and we see that "main" is one, they more or less aren't used except for main, though, as far as I can tell, but its there if you need it for some reason.


    Tomorrow, I'll do a double update to make up for that missed update a couple nights ago, but for now,
that's it for tonight.Questions welcome! Comment, follow, subscribe, share etc, and see you tomorrow!



    And as part of a shameless plug for a friend, if you're interested in classic movies/books/music, visit his site here (fixed), and feel free to throw loads of criticism at us. 



14 comments:

  1. Very informative *goes to check it out*

    ReplyDelete
  2. This is informative indeed, I'm trying to catch up to understand all of this, looking forward to the weekend when I'll have a little marathon of learning coding, any tips of how to learn a little bit faster than experimenting?

    ReplyDelete
  3. Honestly, unless you're especially smart, just reading up on it won't help you. It'll tell you how things work and stuff, but you don't truly see how it works unless you practice. It's kind of like maths in that regard.

    However, when you do practice with small programs, knowing how things like objects and classes differ, and applying these concepts to a preliminary plan, you can practice much faster. Kind of like making blueprints, instead of hitting nails into wood until something works.

    ReplyDelete
  4. One of the languages I never learned, I feel both good that Im not into programming and bad (Im in hardware haha).

    ReplyDelete
  5. I like static methods for classes, such as GetAll() etc, they come in useful and you don't have to instantiate an object.

    ReplyDelete
  6. This is just what i need, thanks for this :D

    ReplyDelete
  7. java isnt my thing, but great progress! +followed, come check my blog out when you get the chance! http://mightyideas.blogspot.com

    ReplyDelete
  8. This is like reading in chinese to me. Ok, not chinese, but probably like reading german while drunk.

    ReplyDelete
  9. looks like you are making good progress.

    ReplyDelete
  10. Why users still use to read news papers when in this technological
    globe everything is available on net?

    My website; paleo rezept hackfleisch

    ReplyDelete
  11. Wonderful blog! I found it while browsing on Yahoo News.
    Do you have any tips on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! Cheers

    My webpage; Anlageberatung in Wiesbaden

    ReplyDelete
  12. Thanks in favor of sharing such a nice opinion, article is
    fastidious, thats why i have read it entirely

    Feel free to surf to my web-site granatapfel gesund

    ReplyDelete