Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
JavaFX | FileChooser Class
Next article icon

Java Class File

Last Updated : 16 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A Java class file is a file containing Java bytecode and having .class extension that can be executed by JVM. A Java class file is created by a Java compiler from .java files as a result of successful compilation. As we know, a single Java programming language source file (or we can say .java file) may contain one class or more than one class. So if a .java file has more than one class then each class will compile into a separate class file.

For Example: Save the below code as Test.java on your system.

Java
// Compiling this Java program would  // result in multiple class files.   class Sample  {   }   // Class Declaration  class Student  {   }  // Class Declaration  class Test  {  	public static void main(String[] args)  	{  		System.out.println("Class File Structure");  	}  }  


For Compiling:

javac Test.java

After compilation there will be 3 class files in corresponding folder named as:

  • Sample.class
  • Student.class
  • Test.class

A single class file structure contains attributes that describe a class file.

Representation of Class File Structure

ClassFile  {      magic_number;      minor_version;      major_version;      constant_pool_count;      constant_pool[];      access_flags;      this_class;      super_class;      interfaces_count;      interfaces[];      fields_count;      fields[];      methods_count;      methods[];      attributes_count;      attributes[]; }

Elements of Class File:

1. magic_number

The first 4 bytes of class file are termed as magic_number. This is a predefined value which the JVM use to identify whether the .class file is generated by valid compiler or not. The predefined value will be in hexadecimal form i.e. 0xCAFEBABE.

Now let’s see what happen when JVM will not find valid magic number. Suppose we have a .java file named as Sample.java as follows and follow step by step process on your system.


// class Declaration class Sample {    public static void main(String[] args)    {        System.out.println("Magic Number");               } }


Step 1: Compile using javac Sample.java
Step 2: Now open the Sample.class file. It will looks like as shown in below image:

Sample Class File

Step 3: Now erase at least single symbol from this Sample.class file from starting of file and save it.
Step 4: Now try to run this using java Sample command and see the magic i.e. you will get run time exception (See the highlighted text in below image):

Runtime Exception While executing .class file

Note: This can vary depending on how much you remove the .class file data.

2. minor_version & major_version

These both together represents .class file version. JVM will use these versions to identify which version of the compiler generates the current .class file. We denotes the version of class file as M.m where M stands for major_version and m stands for minor_version.

Note: Lower version compiler generated .class file can be executed by high version JVM but higher version compiler generated .class file cannot be executed by lower version JVM. If we will try to execute we will get run time exception.

This demonstration is for Windows OS as follows:

Step 1: Open a command prompt window and try to check java compiler version and JVM version using following commands respectively (Highlighted text in image are the commands)
Output for 1.8 version will be:

Java Compiler VersionJVM Version

Step 2: Now check with another version which may be higher or lower than already installed. Check this download link. And install this to your PC or laptops and note the installation address.

Step 3: Open a second command prompt window and set the path of bin folder of installed jdk installed during 2nd step. And check for Java compiler version ad JVM version.

Checking Version of JDK 1.6

Step 4: Now on 1st command prompt compile the any valid .java file. For example: See above Sample.java file. Compile it as:

Compiling with Compiler Version 1.8

Step 5: Now on 2nd command prompt window try to run the above compiled code class file and see what happen. There is a run time exception which we have highlighted in below image.

Runtime Exception Due to Invalid major and minor Version of class file

Note: Internally jdk 1.5 version means 49.0 and 1.6 means 50.0 and 1.7 means 51.0 etc. class file version where the digits before the decimal point represent the major_version and digits after decimal point represents the minor_version.

Below are the remaining elements of .class file.

3. constant_pool_count: It represents the number of the constants present in the constant pool (When a Java file is compiled, all references to variables and methods are stored in the class’s constant pool as a symbolic reference).

4. constant_pool[]: It represents the information about constants present in constant pool file.

5. access_flags: It provide the information about the modifiers which are declared to the class file.

6. this_class: It represents fully qualified name of the class file.

7. super_class: It represents fully qualified name of the immediate super class of current class. Consider above Sample.java file. When we will compile it, then we can say this_class will be Sample class and super_class will be Object class.

8. interface_count: It returns the number of interfaces implemented by current class file.

9. interface[]: It returns interfaces information implemented by current class file.

10. fields_count: It represents the number of fields (static variable) present in current class file.

11. fields[]: It represent fields (static variable) information present in current class file.

12. method_count: It represents number of methods present in current class file.

13. method[]: It returns information about all methods present in current class file.

14. attributes_count: It returns the number of attributes (instance variables) present in current class file.

15. attributes[]: It provides information about all attributes present in current class file.



Next Article
JavaFX | FileChooser Class
author
anshul_aggarwal
Improve
Article Tags :
  • Java
  • java-basics
Practice Tags :
  • Java

Similar Reads

  • Java File Class
    Java File class is a representation of a file or directory pathname. Because file and directory names have different formats on different platforms, a simple string is not adequate to name them. Java File class contains several methods for working with the pathname, deleting and renaming files, crea
    6 min read
  • Java FileWriter Class
    Java FileWriter class of the java.io package is used to write data in character form to a file. The FileWriter class in Java is used to write character-oriented data to a file. It is a character-oriented class that is used for file handling in Java. This Class inherits from OutputStreamWriter class
    5 min read
  • Java FileReader Class
    FileReader in Java is a class in the java.io package which can be used to read a stream of characters from the files. Java IO FileReader class uses either specified charset or the platform's default charset for decoding from bytes to characters. 1. Charset: The Charset class is used to define method
    3 min read
  • JavaFX | FileChooser Class
    FileChooser class is a part of JavaFX. It is used to invoke file open dialogs for selecting a single file (showOpenDialog), file open dialogs for selecting multiple files (showOpenMultipleDialog) and file save dialogs (showSaveDialog). FileChooser class inherits Object class. Constructor of the clas
    5 min read
  • Java FileDescriptor Class
    java.io.FileDescriptor class in Java works for opening a file having a specific name. If there is any content present in that file it will first erase all that content and put "Beginning of Process" as the first line. Instances of the file descriptor class serve as an opaque handle to the underlying
    4 min read
  • Java FilePermission Class
    java.io.FilePermission class represents access to a file or directory. These accesses are in the form of a path name and a set of actions associated with the path name(specifies which file to open along with the extension and the path). Example: In FilePermission("GEEKS.txt", "read") "GEEKS.txt" is
    4 min read
  • Java FileInputStream Class
    FileInputStream class in Java is useful for reading data from a file in the form of a Java sequence of bytes. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader. Example: FileInputStream class to read data from f
    4 min read
  • ClassLoader in Java
    The Java ClassLoader is an integral part of the Java Runtime Environment (JRE) that dynamically loads Java classes into the Java Virtual Machine (JVM). The Java run time system does not need to know about files and file systems because of classloaders. Java classes aren’t loaded into memory all at o
    7 min read
  • Java URL Class
    URL class in Java is a part of java.net package that makes it easy to work with Uniform Resource Locators (URLs). URL is simply a string of text that identifies all the resources on the internet, telling us the address of the resource, how to communicate with it, and retrieve something from it. This
    4 min read
  • Java Reader Class
    Reader class in Java is an abstract class used for reading character streams. It serves as the base class for various subclasses like FileReader, BufferedReader, CharArrayReader, and others, which provide more efficient implementations of the read() method. To work with the Reader class, we must ext
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences