What is class file in Java - How to create Class File

What is class file in Java
Class file in Java is compiled form of Java source file. When we compile Java program which is written in Java source file  ended with .java extension, it produces one more class files depending upon how many classes are declared and defined in that Java source file. One Java source file can only contain one public class and its name must match with name of file e.g. HelloWorld.java file can contain a public class whose name should be HelloWorld as shown below :

public class HelloWorld {

   public static void main(String... args){
      System.out.println("I am inside java class HelloWorld");


if you compile this Java file by

=> javac HelloWorld.java

it will produce

=> HelloWorld.class

Class file details in Java

A class file in Java has .class extension and contains bytecode which is instruction for Java Virtual Machine, which then translates that bytecode into platform specific machine level instruction based upon whether Java program is running on Windows or Linux. In fact this combination of class file, bytecode and JVM makes Java achieves platform independence.If any one asks What is byte code in Java, is it machine instruction? you can answer them that it's just meant for JVM and not for a machine.

When you run a Java program as described in this step by step tutorial for running java program using java command, we provide the name of the class file which contains the main method in Java. JVM first loads that file and executes the main method which is the entry point of  Java application. Remember java compiler  or javac command is used to create a class file from java source file and java command is used to run Java program stored in a class file.

Since class file contains bytecode in hex format and format of the class file is well documented any one can temper with class file and break Java security grantees. In order to prevent that every Java class file is verified by Verifier after loading during Byte code verification process and Verifier rejects the class file which  violates constraints of Java programming language.

What is Class file in Java How to create Class fileThat's all on What is Class file in Java and How to create a class file in Java. In short Class file is a binary file which contains bytecode and Java compiler is used to crate Class file in Java.

Further Learning
Data Structures and Algorithms: Deep Dive Using Java
Java Fundamentals: The Java Language
Complete Java Masterclass

Other Java tutorial you may find useful


  1. Don't work from very beginning!!! command javac HelloWorld.java gets error!!!! Impossibe to create class file with
    javac. Can only do object file or binary execulable if set --main=... Nothing bin code created! I can't undestand why????!!!!!

  2. public class HelloWorld {
    public static void main(Sting[] args) {
    System.out.println("Hello World");

  3. make sure when you compile file is in same directory as command prompt

    1. Yes, and if the class is in a package then you must compile from top level directory.


Feel free to comment, ask questions if you have any doubt.