• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Method References

 
Author
Posts: 6055
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Author
Posts: 6055
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those are anonymous classes defined within Foo. The compiler generates names for them.
 
Mark Herschberg
Author
Posts: 6055
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic