Newsletter sign-up
View all newsletters

Sign up for our Enterprise Java Newsletter

Enterprise Java

The Java class file lifestyle

An introduction to the basic structure and lifestyle of the Java class file

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Welcome to another installment of "Under the Hood." In last month's article I discussed the Java Virtual Machine, or JVM, the abstract computer for which all Java programs are compiled. If you are unfamiliar with the JVM, you may want to read last month's article before this one. In this article I provide a glimpse into the basic structure and lifestyle of the Java class file.



Born to travel

The Java class file is a precisely defined format for compiled Java. Java source code is compiled into class files that can be loaded and executed by any JVM. The class files may travel across a network before being loaded by the JVM.



In fact, if you are reading this article via a Java-capable browser, class files for the simulation applet at the end of the article are flying across the Internet to your computer right now. If you'd like to listen in on them (and your computer has audio capability), push the following button:



You need a Java-enabled browser to view this applet



Sounds like they're having fun, huh? That's in their nature. Java class files were designed to travel well. They are platform-independent, so they will be welcome in more places. They contain bytecodes, the compact instruction set for the JVM, so they can travel light. Java class files are constantly zipping through networks at breakneck speed to arrive at JVMs all over the world.



What's in a class file?

The Java class file contains everything a JVM needs to know about one Java class or interface. In their order of appearance in the class file, the major components are: magic, version, constant pool, access flags, this class, super class, interfaces, fields, methods, and attributes.

Information stored in the class file often varies in length -- that is, the actual length of the information cannot be predicted before loading the class file. For instance, the number of methods listed in the methods component can differ among class files, because it depends on the number of methods defined in the source code. Such information is organized in the class file by prefacing the actual information by its size or length. This way, when the class is being loaded by the JVM, the size of variable-length information is read first. Once the JVM knows the size, it can correctly read in the actual information.



Information is generally written to the class file with no space or padding between consecutive pieces of information; everything is aligned on byte boundaries. This helps keeps class files petite so they will be aerodynamic as they fly across networks.



The order of class file components is strictly defined so JVMs can know what to expect, and where to expect it, when loading a class file. For example, every JVM knows that the first eight bytes of a class file contain the magic and version numbers, that the constant pool starts on the ninth byte, and that the access flags follow the constant pool. But because the constant pool is variable-length, it doesn't know the exact whereabouts of the access flags until it has finished reading in the constant pool. Once it has finished reading in the constant pool, it knows the next two bytes will be the access flags.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (5)
Login
Forgot your account info?

the strong protecting theBy csy211 on October 12, 2009, 11:32 pmthe strong protecting the weak, as she had protected the lost brood of ducklings with her foreleg on the night of Major’s speech. Instead -- she did not know why-they...

Reply | Read entire comment

and for a few moments theyBy csy211 on October 12, 2009, 11:30 pmand for a few moments they appeared to go quite mad. To the amazement of everybody three of them flung themselves upon Boxer. Boxer saw them coming and put out his...

Reply | Read entire comment

hiBy Anonymous on September 23, 2009, 2:30 pmhello i we like to be with this site

Reply | Read entire comment

java classBy Anonymous on February 25, 2009, 12:13 pmwhen compiling the public static void main does not work

Reply | Read entire comment

java classBy Anonymous on November 25, 2008, 11:51 amYou can read good java related questions at http://www.java4all.info

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • The Java Virtual Machine Specification, the official word from Sun.
    http://java.sun.com/1.0alpha3/doc/vmspec/vmspec_1.html
  • When it comes out, the book The Java Virtual Machine Specification, http://www.aw.com/cp/lindholm-yellin.html, by Tim Lindholm and Frank Yellin (ISBN 0-201-63452-X), part of The Java Series, http://www.aw.com/cp/javaseries.html), from Addison-Wesley, will likely be the best JVM resource.
  • A draft of chapter 4 of The Java Virtual Machine Specification, which describes the class file format and bytecode verifier, can be retrieved from JavaSoft.
    http://java.sun.com/java.sun.com/newdocs.html