• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method References

 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Larry LeFever
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Peter Haggar
author
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those are anonymous classes defined within Foo. The compiler generates names for them.
 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic