How exactly are class files structured? I'm fairly certain that a class file includes its method definitions. What about inherited methods? For some reason I think that the class file contains only references to the parent's methods. Is this correct, or do the methods actually get copied into the child class? In either case, are there different costs for methods which are declared in a class, as opposed to inherited, during compilation, class loading, or method invocation?
To learn more about the .class file structure, have a look at chapter 5?(I think it's 5) of the JVM spec. Anyway, a .class file contains the bytecode for the compilation unit. Therefore, if I have a .java file that contains a base and derived class, all the methods will be in that .class file. However, if I have the base and derived classes in different .java files, I will get 2 .class files. The .class file for the derived class will not contain the bytecode for the methods of the base class that it inherits. The cost for invoking a method can differ if the method requires dynamic resolution. A static method is invoked with the invokestatic opcode where a method that requires dynamic resolution will use the more expensive invokevirtual opcode. Peter Haggar ------------------ Senior Software Engineer, IBM author of: Practical Java
"Therefore, if I have a .java file that contains a base and derived class, all the methods will be in that .class file. " Really? Since when is the definition of more than one class written into any given class-file? Wouldn't that create redundancies? There's a difference, isn't there, between the derived class's method-table containing entries for the methods it inherits vs. the derived class's definition (in the class-file) containing the byte-codes of those inherited methods? Please clarify.
Sun-Certified Java Programmer
Joined: Jan 03, 2001
I was wrong...my apologies. If two classes are defined in a single file, the compiler will generate two .class files, one for each. However, the inherited methods are not copied to the derived class's .class file. They are referenced from there, if they are called, but the code exists in the .class file for where the method is defined. Peter Haggar ------------------ Senior Software Engineer, IBM author of: Practical Java
Joined: Dec 04, 2000
I've noticed that sometimes a java file turns into two or more class files, e.g. Foo.java becomes Foo.class, Foo$1.class, Foo$2.class, etc. Why does this happen and what does it mean? Is it that the cass file is just very large and gets split? This seems to sometimes happen even for relatively small files. --Mark
Those are anonymous classes defined within Foo. The compiler generates names for them.
"I'm not back." - Bill Harding, Twister
Joined: Dec 04, 2000
Originally posted by Jim Yingst: Those are anonymous classes defined within Foo. The compiler generates names for them.
I think sometimes I get those even when I haven't defined an anonymous class. Will compilers sometime generate them? I guess they might do so with listeners. Any other times when compilers might (I realize this may vary from one compiler to the next)? --Mark