On 6/17/2010 12:24 PM, Shiv Shankar wrote:
Hi everyone,
This look like silly questions, but I am confused and asking this
questions. "What is size of an empty class ? ".
We know in C/C++ its one byte. In java as there is no empty class
(Object class inherited automatically), what will be the size of class
if I am not providing any code in the class definition.
An empty class isn't as empty as one with a private nullary constructor.
That is, this:
class E {
private E() {
}
}
is smaller than: class E{}, because by default every class has a public
nullary (zero argument) constructor.
Also, the name of the class affects the .class file size.
But a minimal-size class like this cannot be instantiated, there is
nothing to inherit, nothing static to reference, etc, etc.
Except when referenced by something like Class.forName the notion of the
"size of this class" is not meaningful.
Also, the memory used to represent this class with a class object if it
is referenced is implementation dependent, but most likely it will be an
"empty" object. That is, "how big is the smallest object?" and "how big
is the smallest class?" probably have the same answer if you add "when
referenced" to the question. In the case of the class, until it is
referenced it does not exist except in the mind of the programmer and on
disk as a .class file by itself or in an archive.
Finally, note that the Object.hashCode() for a class object or any other
object is not guaranteed to be a unique value. So this removes one more
answer to the "why would you need an empty class?" question.
Regards,
Pete
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en