Motivating static
Let’s motivate static
data with an example. Suppose that we have a video game with Martian
s and other space creatures. Each Martian
tends to be brave and willing to attack other space creatures when the Martian
is aware that at least four other Martian
s are present. If fewer than five Martian
s are present, each of them becomes cowardly. Thus, each Martian
needs to know the martianCount
. We could endow class Martian
with martianCount
as an instance variable. If we do this, then every Martian
will have a separate copy of the instance variable, and every time we create a new Martian
, we’ll have to update the instance variable martianCount
in every Martian
object. This wastes space with the redundant copies, wastes time in updating ...
Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.