Home > Default > Static Class Function vs. Instance Variables

Static Class Function vs. Instance Variables

November 30Hits:0
I'm making a Wheel class to spin the wheels on some toy
trains as they move back and forth.
Each wheel on the train is an instance of the Wheel class and
there are several of them.
I thought it would be great to just have a static class
function to tell all the Wheels to start turning:
The Wheel class keeps a static array of all of its instances
so I thought I would just loop through all of those instances and
issue the wheelInstance.roll() method.
So far it all works. But I was planning to use a setInterval
to call the roll() method and each instance has its own rollID
property that I would like to assign the setInterval ID to. Here is
the problem.
Since the rollID is an instance property I can't access them
from a static class function. Is there any way to do this?
Currently I"m just using an onEnterFrame which doesn't require me
to use the instance properties.


Technically yes, realistically for this class no.A
class will probably take several hundred bytes at
least to load, with no data of your own. So adding4
bytes for a int is less than 1% of the total size.
And if you are loading millions of differentclasses
then you should rethink your design.If you don't instantiate the class when you reference
a static variable why would you consume memory for the
class other than the variable itself? I don't
understand what you are talking about with the
"millions of different classes", it's not germane to
the question. Bottom line, referencing a static
variable more than once will save memory.Using a class, static or by instance, requires that the class be loaded. A loaded class creates, at the very least, an instance of java.lang.Class. Any static members of the class are in addition to the storage space needed for the instance of java.lang.Class and for any internal storage needed by the JVM in addition to that.
Thus if one has a static data member when the class is used in any way, the static data member takes storage space. However a member (non-static) does not take storage space.
Of course the meta data for the member could take as much space as the static member so the point could be moot. Is that what you were referring to?

Read other 14 answers


Related Articles

Copyright (C) 2019 wisumpire.com, All Rights Reserved. webmaster#wisumpire.com 14 q. 0.522 s.