Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes Plz . Answer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Plz . Answer" Watch "Plz . Answer" New topic

Plz . Answer

faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
What is the size of an object in Java , if the class has no member variables or member functions .

Eg :-

class ABC


What will be the size of object of class ABC if we do

ABC ob1 = new ABC();

Do we have a sizeof operator in Java as we have in C/C++
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

The actual amount of memory that an object uses depends on the Java implementation -- i.e., it's not specified by the Java VM specification or the Java Language specification. The number is never directly useful in any program -- no Java language function or construct depends on the memory image size of an object. The precise answer may well vary by JVM vendor, by platform, and even by version.

That said, for Sun's JVMs a good rule of thumb is 16 bytes for an object, plus four bytes for each member -- except doubles and longs, which are 8 bytes each.

Note: have a look at this when you have time.

[Jess in Action][AskingGoodQuestions]
faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
Thanks sir
I got the point
I agree. Here's the link:
subject: Plz . Answer
It's not a secret anymore!