Python/Java OOish type question
Kirby Urner
urner at alumni.princeton.edu
Thu Aug 31 12:14:42 EDT 2000
>C++ works in a similar way. I'm very surprised to hear that in Java the
>number of methods defined for a class affects the size of the instances. Am
>I understanding you correctly?
Possibly I'm misconstruing the Java implementation.
The purpose of declaring methods 'static' is to have them belong
to the class. But 'static' methods can't refer to any 'this'
variables (things unique to an instantiation). So I can't
write:
class quat {
int value;
public quat(int v){ // constructor, like __init__
value = v; // instantiated per object
}
static public int add(quat b){ // won't work as 'static'
return new quat(value + b.value)
}
}
This confusion goes back to a discussion I had a long time
ago with the webmaster of a site on quaternions. He said
he was reluctant to define his methods internally to the
quaternion class (in Java) because he didn't want to waste
memory when he created a whole lot of them. But I definitely
want quaternion objects to grok methods internally.
Looks like I may understand how Python works, but still need
to research my Java some more. In particular, I need to
understand how declaring methods 'static' does or does not
impact the size in memory of instantiated objects based on
that class.
I should go back to my 'Thinking in Java' by Eckles perhaps....
Yeah, I think he clears it up for me:
While static, when applied to a data member, definitely
changes the way the data is created (one for each class vs.
the non-static one for each object), when applied to a
method its not so dramatic. An important use of static
for methods is to allow you to call that method without
creating an object. This is essential, as we will see,
in defining the main( ) method that is the entry point
for running an application.
The purpose of making methods 'static' is not to conserve
memory, but to make the method executable without necessarily
instantiating any objects of that class. But I can reasonably
assume that even non-static methods, as in Python, do not
bloat the memory content simply because I create more objects
of that class.
Thanks for the comments, I think I'm clear now.
Kirby
More information about the Python-list
mailing list