aspose file tools*
The moose likes Performance and the fly likes Method References Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Method References" Watch "Method References" New topic
Author

Method References

Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037

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?

--Mark
hershey@vaultus.com
Peter Haggar
author
Ranch Hand

Joined: Jan 03, 2001
Posts: 106
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


Senior Software Engineer, IBM
author of: Practical Java
Larry LeFever
Greenhorn

Joined: Jun 13, 2000
Posts: 18
"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
Peter Haggar
author
Ranch Hand

Joined: Jan 03, 2001
Posts: 106
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
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
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
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Those are anonymous classes defined within Foo. The compiler generates names for them.


"I'm not back." - Bill Harding, Twister
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
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
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
You are probably generating anonyouns classes for your listeners, but hadn't realized. If you say

then you are declaring an anonymous class. Is this the kind of thing which is causing this to happen in your code?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
 
 
subject: Method References