Showing posts with label Static. Show all posts
Showing posts with label Static. Show all posts

Thursday, 6 October 2011

Java: Inheritance IV

Today, we get to talk about the super keyword, and how static variables work with inheritance!


Super


Remember our Car had an int parameter, for whatever reason. Well if we had a constructor for Vehicle with an integer parameter that we wanted to use, our Car constructor would throw an error.. This is because a default constructor is looked for, but not found (we only have an integer parameter one). What we need to do instead is call the superclass constructor with either a parameter in the Car constructor, or some other value.






The super expression (heh) replaces the automatic call to the superclass constructor so the superclass constructor with the matching parameter is called instead. Just remember that if you are going to use super, it should be the first statement in your subclasses constructor.




Statics


Since static variables and methods are inherited, we should probably know how they work when inherited. Well, pretty much the same as always, 1 variable is shared between all instances of a class, including subclasses. It doesn't work in reverse though. A subclass is an extension of its superclass, but a superclass is not an extension of a subclass. Example:






...would lead to each new instance of Vehicle AND Car to increase the count, since Car uses the Vehicle constructor, but a similar counter inside Car would not increase for a Vehicle being created.




I'm tired now, so that's it for now..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. 

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.