Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Well that was simple enough! I haven't come across a situation where multiple values were instantiated this way in my studies. I'll just remember to look @ the last value if I see it again. After looking over my notes, I did have another example of ivars (using the same code) and so the author just left it in the example for the class var.
And just so I'm clear about access, a static variable/method cannot access a non static and vice versa?
a static cannot access a non-static, but it is not true the other way around. a static variable exists whether an instance is created or not. it is always there, so you can reference it from a static or non-static methods.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Do you understand what static means exactly with regard to member variables?
It means that there is only one copy of the variable that is shared by all instances of the class, rather than that there is a separate copy of the variable for each instance (which is the case for non-static member variables). So:
y is a static member variable, which is shared by all Thing objects. If you change the value of Thing.y, as you are doing in lines 22, 23 and 24, then you are changing the one variable that exists that is shared by all Thing objects. It doesn't matter on which instance of Thing you do this (you're doing it for three different Thing instances) - you're still modifing the one shared static variable.
Lines 26 to 29 are just printing the exact same variable four times, so you see the same value four times.
Note that it is bad style to refer to static member variables or static methods via instances of the class; you should really access them using the classname, not using a specific instance of the class. For example:
Accessing static members through an instance is bad style, because it hides the fact that you're accessing a member that is shared between all instances of the class - it looks like you're accessing a variable of just that instance, which is confusing. (In my opinion, it should not even have been allowed in the Java language that this is possible - I don't see any use case where you'd ever need to access a static member through an instance).
Thanks Jeng! I do understand the sharing distinction between the two. You do have a great way of explaining it though and I wish I had seen it a few days ago! Earlier today, I did not understand why the last variable was the answer. But understanding what I do now, it made sense because it was a static and not an instance variable.
As for the sloppy code...I can't take responsibility for that directly. I copied it from a worksheet from the Stanford Java videos ;) And here Professor Sehami is always talking about writing presentable code! hmph
I have to take the Java Programmer cert by next month and I know I shouldn't be cramming, but I don't have a choice. So, I will probably be posting quite often ;) I was dreaming about code last night...wonder what that means??? haha.