When can a .class file get created in Java?


A Java class file has a ".class" extension and contains the Java bytecode. This class file can be executed by the Java Virtual Machine (JVM). A  ".class" file is created as a result of successful compilation by the Java compiler from the ".java" file. Each class in the .java file is compiled into a separate class file if the ".java " file has more than one class.

Example

class A {
   A() {
      System.out.println("This is class A");
   }
}
class B {
   B() {
      System.out.println("This is class B");
   }
}
class C {
   C() {
      System.out.println("This is class C");
   }
}
public class ClassTest {
   public static void main(String[] args) {
      A obj1 = new A();
      B obj2 = new B();
      C obj3 = new C();
   }
}

In the above example, after a successful compilation of a Java program, there are four ".class" files are created in the corresponding folder as there are four classes are defined in the "ClassTest.java" file. Those are A.class, B.class, C.class and ClassTest.class.

Output

This is class A
This is class B
This is class C

Updated on: 03-Jul-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements