Why are we using a Static variable here?

Hey guys!

I’m having the hardest time getting my head around what a Static variable is.
I’ve re-watched the video a couple times, I’ve read the Wikipedia article on Static variables are, but I’m having the hardest time understanding why the breakableCount variable isn’t just an int.

I’m sure the videos went over it at some point in time (this one has the narration: “to revise what we talked about statics before”) so I’m just being dumb and not remembering/paying attention, but I hoped one of you guys could lay it out for me :slight_smile:

Thanks in advance!

Hi Xenophule,

==> You may already know this given nearly a month has passed, BUT:
A static variable is basically a class-wide variable. Normally, a variable is kept by it’s own “object” or instance (for example, every brick tracks how many times it has been hit/can be hit). HOWEVER, a static variable allows us to have one value shared throughout the class, such as breakableCount: The number of breakableBricks left in the level.

==> static variables/methods are referred to by their class name rather than a variable as well, so if a variable/method is static and accessible (e.g. a pubic static int, such as breakableCount), then we can refer to it anywhere by using [ClassName].[StaticVariable] (e.g. Brick.breakableCount).

==> breakableCount is a public static int because we’re making the total number of bricks known to the class.

==> We can add to this in start, when an individual brick is brought into existence, and we can decrement it when a brick is destroyed.

==> The public part allows us to use Brick.breakableCount to see how many bricks are remaining while we’re in LevelManager.

Hope this helped :slight_smile:
… It probably didn’t; you’ve had a month. But I’ll still leave this here for other people, just in case :wink:


Hey hey!

I actually hadn’t figured this out yet, so your reply has helped loads!

Thank you for helping me out :slight_smile:

Privacy & Terms