Thursday, May 25, 2017

Class file in Java - things to know for programmer and developer

.class file in Java is the core of achieving platform independence which is a key feature of Java. class files are compiled form of any Java program written in byte codes. Since Java program executes inside JVM, these instructions are read by JVM and then translated into machine code for the underlying platform. effectively you can run the class file on any platform if you have a corresponding JVM supported for that platform. Java compilers are responsible for generating .class file and they follow class file format specified by Sun and it's different based on different Java versions.

This article is not a detailed one focused on class file format instead it contains basic information about class files and intended to give an idea about what is a class file in Java and what are some important points related to class files.

This Java article is in continuation of my series of 10 points of the interface in Java and 10 Object-oriented design principles for Java programmer. If you haven’t got a chance to read them you may find them useful.

And, if you are serious about improving your advanced JVM skill and learn things like taking and analyzing heap dumps then highly recommend you to join Java Application Performance and Memory Management course on Udemy. It's one of the advanced courses for Java programmers to learn more about Performance and Memory management including troubleshooting memory leaks in Java.

What is the class file in Java?

basics of .class file format in JavaIn general, every class file in java contains a definition of a single class or interface in a stream of 8-bit bytes. Every .class file follows a predefined class file format, failing which .class file will be deemed invalid and not run on any JVM or JRE. the class file is generated by Java compilers and executed by Java virtual machine and its byte code is verified during the class loading process, which addresses the possibility of altering .class file externally. 

One of the reasons Java is considered relatively safe and secure language is its ability to verify class file byte codes before executing it. a class file contains 10 basic sections which specify all data and metadata like magic numbers, major and minor version of a class file, etc. Read official VM spec for a class file format for more details on how a .class file looks like.

Important points about a class file in Java

1. class file in java is generated when you compile .java file using any Java compiler like Sun's javac which comes along JDK installation and can be found in JAVA_HOME/bin directory.

2. class file contains byte codes. byte codes are special platform-independent instructions for Java virtual machines. bytecode is not a machine language and mere instruction to JVM. since every machine or processor e.g. INTEL or AMD processor may have different instructions for doing the same thing, it's left on JVM to translate bytecode into machine instruction and by this way java achieves platform independence.

3. class file of the inner class contains $ in their name. So if a Java source file contains two classes, one of them is inner class than the java compiler will generate two class file. the separate class file for the top-level and inner class. you can distinguish them by looking at there name. Suppose you have top level class as "Hello" and inner class as "GoodBye" then java compiler will generate two class file:


Hello$GoodBye.class is a class file for inner class. whose name is in format of top-class$inner-class.

4. You can look bytecode of class file using javap command. javap command can also display method and field information from .class file. see my post how to decompile .class file in Java for more details.

5. class file format is subject to change and its changed to support new feature introduced in Java 1.5. In general every java compiler and JRE comes with supported version of .class file format and you can not run a .class file which is in higher version of those supported by JRE. this often result in java.lang.UnsupportedClassVersion. class file has two version major and minor which is included inside class file. See my post Bad version number in .class file for more details.

6. .class file in java is identified by a magic number in header which is a 4 byte CA FE BA BE ( in hex). which is the first element in .class file, followed by major and minor versions of class file

7. .class file allows Java Virtual machine to implement security. Since number of instruction class file contains are limited. also if some one tempers .class file externally it may not run on different browsers and JRE.

8. Java class file can be divided into 10 basic sections e.g.
Magic Number : 0xCAFEBABE
Major and Minor class version or .class file
Constant pool - constants for this class
Access modifiers - to indicate whether class is static or abstract
This class - name of this class
Super class - name of super class
Interfaces -

9. Top level .class file name is same as name of java source file e.g. will produce Hello.class

10. You can view class file in any hex editor or any byte code viewer plug-in in Eclipse.

That's all on what is class file and basics of .class file in Java. As I said this is not a detailed article on .class file format rather this is the bare minimum information regarding .class file an average java programmer should be familiar with. I highly recommend to read more about .class file format if you are interested but it depends upon your experience level and beginners may not find the class file format easy to understand.

Further Learning
Java Memory Management
Understanding the Java Virtual Machine: Memory Management

Java Performance The Definitive Guide

Related tutorials on Java

1 comment :

Unknown said...

could you please give a detailed information about .class file in java.

Post a Comment