What is a Java Class File? - Programmer's Academy

What is a Java Class File?

A Java class file produced by a Java compiler contains Java bytecode executed on the Java Virtual Machine. Java programming language source files that contain java classes are responsible for creating Java class files.

Java source code compiled into class files is an accurately defined format for compiled Java. The class files can travel across a network before being loaded with the JVM.

There is a difference between the class file and the .java file. A .class file we use when we deploy our applet consists of the Java bytecode, while a .java file contains Java source code.

Each class is compiled into a separate class file if a source file has more than one class.

Key points to understand Java Class File:

  • The Java class file contains Java bytecode and can be executed by JVM.
  • Each bytecode instruction can be stored into one byte.
  • When the Java compiler compiles the java source file with the help of the .class extension can be converted into a Java class file.
  • A Java program has several classes.
  • .class file name is the same as the class name. Each class can be separately stored.
  • A class file contains a stream of 8-bit bytes.
  • Java class files are designed to travel swiftly.
  • These files are platform-independent and can be used in more than one place.
  • Since class files contain bytecodes, they can travel fast.
  • The actual length of the information stored in the class file varies in length.
  • When loading a file, JVMs knows what exactly to expect because the order of class file parts are precisely defined

Here are  the examples of a class file:

Here are the code of a class file in HTML, CSS:

<a class="btn btn-primary" data-bs-toggle="offcanvas" href="#offcanvasExample" role="button" aria-controls="offcanvasExample">
  Link with href
</a>
<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasExample" aria-controls="offcanvasExample">
  Button with data-bs-target
</button>

<div class="offcanvas offcanvas-start" tabindex="-1" id="offcanvasExample" aria-labelledby="offcanvasExampleLabel">
  <div class="offcanvas-header">
    <h5 class="offcanvas-title" id="offcanvasExampleLabel">Offcanvas</h5>
    <button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body">
    <div>
      Some text as placeholder. In real life you can have the elements you have chosen. Like, text, images, lists, etc.
    </div>
    <div class="dropdown mt-3">
      <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown">
        Dropdown button
      </button>
      <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
        <li><a class="dropdown-item" href="#">Action</a></li>
        <li><a class="dropdown-item" href="#">Another action</a></li>
        <li><a class="dropdown-item" href="#">Something else here</a></li>
      </ul>
    </div>
  </div>
</div>

Major Parts of Class File:

The major parts of a class file are constant pool, magic, version, this class, access files, interfaces, superclass, attributes, fields, and methods.

Constant pool:

Constants related to the class file are stored in the constant pool. Some constants are final variables, literal strings, interface names, signature, and related names.

In the class file, constants are shown by the integer index. So while loading the class file, JVMs will know how many constants to expect.

A one-byte tag specifies the type of constant in the array. When JVM interprets this tag, it knows what is next.

For example, if a tag shows the constant is a string, JVM looks to find the length number of bytes.

Magic Numbers:

The first four bytes of a class file are 0xCAFEBABE. It helps Java class files easier to find. Moreover, it can be pulled out of a hat by the file format designers.

As for the second four bytes of the class file, they contain minor and major version numbers. These numbers allow JVMs to identify whether or not a class file be loaded.

This class:

This class component has a one-byte tag and a two-byte name index. Then the tag being equal to CONSTANT Class contains information about interface or class.

Hence, the JVM knows CONSTANT Class elements that a two-byte index into the constant pool follow their one-byte tag.

Access flags:

The access flags show whether the file defines interfaces or a class and whether the class is abstract, public, or interface.

Interfaces: 

An interface is displayed by CONSTANT Class elements in the constant pool. It starts with a two-byte count of the number of interfaces mentioned in the file.

Super Class:

Superclass component is another two-byte index into the constant pool. It is a CONSTANT Class which indicates the name of the superclass.

Attributes:

The attributes section having a two-byte count of the number of attributes gives information about the specific interface or class explained by the file.

For example, each attribute shows the name of the source file. And JVMs automatically ignore the attribute they cannot recognize.

Fields:

A field component that starts with a two-byte count of the number class is a class variable of interface or the class.

The method component, which starts with a two-byte count of the number of methods, is clearly defined by this class.

Each method contains information about the method and the number of stack words required for the method’s operand stack.

Conclusion:

In short, class files in Java can effectively achieve platform independence. It helps to effectively run the class file on any platform if we have a corresponding JVM. It can be changed into a Java class file when the Java compiler compiles the java source file with the .class extension.

Recommended Articles:

  1. File names and Class names in Java
  2. How is the Java platform independent?
  3. How do I become a good Java Programmer
  4. What is Arrays In Java
  5. What is a Constructor in Java?

References:

Help Me Please: If you found any error in the content or example please let us know by comment also write your suggestion or if any doubt in the concept please feel free to write us in the comment section.

Practice your Code online here

(Visited 36 times, 1 visits today)

0 0 votes
Article Rating
Subscribe
Notify of
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

[…] What is a Java Class File? […]

//soaheeme.net/4/4219743
You cannot copy content of this page
Scroll Up
1
0
Would love your thoughts, please comment.x
()
x

  • Name

  • Minimum length of 8 characters.