In response to Aaronaught's response to the question at:
Can't I just use all static methods?
Isn't less memory used for a static method? I am under the impression that each object instance carries around its own executable version of a non-static member function.
Regardless of how much overhead is involved in calling a static method, regardless of poor OO design, and possible headaches down the road, doesn't it use less memory at runtime?
Here is an example:
I make a vector of zero-initialized objects. Each object contains one piece of data (a triangle consisting of nine double's). Each object is populated in sequence from data read from a .stl file. Only one static method is needed. Proper OO design dictates that a method dealing with the the data directly should be distributed to each object. Here is the standard OO solution:
foreach(obj in vec) {
obj.readFromFile(fileName);
}
Each obj carries readFromFile
's compiled code alongside the data!
Memory is more of a concern than performance in this case, and there is a LOT of data on a constrained system.
Solutions:
- Namespace method (great for C++ but not possible in Java)
- One static method in obj's class. Executable code is kept in one place at runtime. There is a small overhead to call the method.
- A parent class from which obj is derived, which contains the private method
readFromFile
. Call withsuper.callPrivateMethod()
which callsreadFromFile
. Messy, and still some memory overhead in each object. - Implement
readFromFile
outside obj's scope, so in vec's class or in the calling class. This, in my opinion, breaks data encapsulation.
I realize for large amounts of data one explicit object for each triangle is not the best approach. This is only an example.