Quick revision
The short answer
The Class-File API, in package java.lang.classfile and final in Java 24 (JEP 484), is a standard JDK API for reading, generating and transforming class files. The JDK added it because ASM and similar libraries have to be updated after every new class file version, so tools built on them lag behind each JDK release, while an API owned by the JDK changes in step with the format. It models a class as immutable elements, builds new classes with builders, and expresses transformations as functions over those elements, and the JDK is moving its own internal uses of ASM to it.
What to remember
In this answer, we saw that the Class-File API in java.lang.classfile is the JDK's own standard way to parse, generate and transform class files, final since Java 24. It exists because ASM must be updated for each new class file version, so tools built on it lag behind every JDK release, while an API owned by the JDK always matches the format of the JDK it ships with. It models classes as immutable elements, builds them with builders that take care of the constant pool and stack maps, and expresses changes as transforms over elements. The JDK has already moved its internal ASM uses to it, and new tools can follow when they no longer need to run on older JDKs.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The Class-File API, in package java.lang.classfile and final in Java 24 (JEP 484), is a standard JDK API for reading, generating and transforming class files. The JDK added it because ASM and similar libraries have to be updated after every new class file version, so tools built on them lag behind each JDK release, while an API owned by the JDK changes in step with the format. It models a class as immutable elements, builds new classes with builders, and expresses transformations as functions over those elements, and the JDK is moving its own internal uses of ASM to it.
Why this question is asked
Most Java developers never write bytecode, but almost all of us depend on tools that do: frameworks that generate proxies, mocking libraries, coverage agents and build plugins. When those tools cannot read a new class file version, a JDK upgrade stops. Interviewers ask about the Class-File API to see whether we understand why that keeps happening and how the JDK's own API changes it. In this answer, we will recap what a class file is and how ASM fits in, explain the version lag, look at the API's model with code, see how transforms work, and discuss when it makes sense to adopt it.
1A Quick Recap: Class Files and ASM
When javac compiles a source file, it writes a class file: a binary format that holds the class name, its fields and methods, the bytecode instructions of each method, and a constant pool of names and types they refer to. Every class file starts with a version number. Java 21 writes version 65, Java 24 writes 68, and Java 25 writes 69, and each version may add new attributes or rules.
ASM is the long-standing open-source library for working with class files. It reads them with a visitor-based API, where ASM calls our methods for each part of the class, and writes them with ClassWriter. Many other tools are built on it, including the bytecode parts of Spring (its repackaged CGLIB), ByteBuddy and Mockito indirectly, and until recently the JDK itself kept a private copy of ASM inside its own code.
2Why ASM Always Lags Behind the JDK
ASM checks the version number of each class it reads. If the version is newer than the ones it knows, it refuses, because it cannot be sure it understands the content. So when Java 25 is released, an application compiled for Java 25 cannot be processed by a tool that bundles an older ASM, until ASM releases a new version, the tool upgrades to it, and the tool itself is released.
7 messages in order. Play them, or step through them one at a time.
With the Class-File API, the middle steps disappear for tools that adopt it.
This chain of releases is the lag. The JDK has a six-month release cadence, so it repeats twice a year, and every library in the chain must act in time. The JDK also suffered from it: its own internal copy of ASM had to be updated for features of the very release being built.
A JDK-owned API removes the chain for tools that use it. The Class-File API in JDK 25 understands version 69 the day JDK 25 ships, because it is part of JDK 25. A tool that uses it on the JDK it is running on can always read the classes that JDK can run.
3The Model: Immutable Elements and Builders
The API treats a class file as a tree of elements. ClassFile.of() gives us a ClassFile object with options. parse(bytes) returns a ClassModel, which is an immutable view of the class: its methods are MethodModel elements, and each method's code is a CodeModel made of instruction elements. Names and types use the descriptors from java.lang.constant, such as ClassDesc and MethodTypeDesc, instead of raw strings like "(I)V".
To generate a class, we call build with a ClassDesc and a lambda that receives a ClassBuilder. The builder adds methods, and each method body receives a CodeBuilder with one method per instruction.
Notice that we never manage the constant pool or compute stack map frames by hand; the API does it, which removes a whole class of subtle bugs that ASM users know well. Because the models are immutable, a parsed class can be shared safely between threads.
4Transforms as Functions Over Elements
Most tools do not create classes from nothing; they change existing ones, for example adding logging to methods or removing debug methods. The Class-File API expresses this as a transform: a function that receives each element of the class in turn and decides what to emit into the new class. Elements we pass through unchanged are copied as they were, and elements we do not emit are dropped.
| Aspect | ASM | java.lang.classfile |
|---|---|---|
| Owner | separate open-source project | the JDK, released with it |
| New class file versions | needs a new ASM release | supported on the day of the JDK release |
| Reading model | visitor callbacks | immutable models of elements |
| Changing classes | chained ClassVisitor subclasses | transforms as functions over elements |
| Runs on | old and new JDKs | JDK 24 and later only |
The same idea works at every level: a ClassTransform sees class elements, a MethodTransform sees method elements, and a CodeTransform sees instructions. Small transforms can be combined with andThen, so a tool can build a pipeline out of simple, testable pieces. Compared with ASM's chain of visitor classes, this reads more like ordinary Java code over a list.
5The JDK Moves to Its Own API
The JDK itself is the first big user. Code inside the JDK that used its private copy of ASM, such as the parts that spin classes for lambdas and some jlink and tool code, has been migrated to the Class-File API across recent releases, with the goal of removing the internal ASM copy entirely; check your version for exactly which parts. This is also how the API's design was tested before it became final: it went through previews in JDK 22 and 23 before JEP 484 finalised it in JDK 24.
6Follow-up questions
Would you rewrite an existing ASM-based agent now? What would you weigh?
Not by default. The main limit is that the API exists only on JDK 24 and later, so an agent that must also run on Java 17 or 21 cannot depend on it, or must keep both implementations. I would weigh how often the version lag has actually blocked us, how large and well tested the agent is, and which JDK versions our users run. A reasonable path is to write new tools on the Class-File API, and to port an existing agent when its supported JDKs are all 24 or later, using its test suite of real class files to check that the output is the same.
Does the Class-File API read class files from future JDKs?
No. It supports versions up to the JDK it runs on, which is exactly the point: it always matches the running JDK. A tool running on JDK 25 can process classes compiled for 25. What disappears is the waiting for a third-party library to catch up.
Will ASM and ByteBuddy go away?
Not soon. They support older JDKs, have huge ecosystems and offer higher-level features; ByteBuddy, for example, generates whole classes from a fluent description. Over time, some of them may use the Class-File API internally when running on new JDKs, which would shorten the lag for everyone who depends on them.
7Conclusion
In this answer, we saw that the Class-File API in java.lang.classfile is the JDK's own standard way to parse, generate and transform class files, final since Java 24. It exists because ASM must be updated for each new class file version, so tools built on it lag behind every JDK release, while an API owned by the JDK always matches the format of the JDK it ships with. It models classes as immutable elements, builds them with builders that take care of the constant pool and stack maps, and expresses changes as transforms over elements. The JDK has already moved its internal ASM uses to it, and new tools can follow when they no longer need to run on older JDKs.
Open with
The Class-File API is a standard API in java.lang.classfile, final since Java 24 under JEP 484, for parsing, generating and transforming class files. The reason it exists, even though ASM has done this job for twenty years, is the version lag.
Then, if they want more
Every class file carries a version number, Java 25 writes 69, and ASM refuses versions it does not know yet. So every six months there is a chain: ASM releases support, then each framework or agent that bundles ASM updates and releases, and only then can teams use the new JDK.
The JDK had the same problem with its own internal copy of ASM. Because the Class-File API ships with the JDK, it supports the new format on the day of release.
The design is also more modern: ClassFile.of().parse gives an immutable ClassModel of elements such as methods and instructions, build takes a lambda over a ClassBuilder and handles the constant pool and stack map frames for us, and transformClass takes a function that sees each element and decides what to emit.
The JDK has been migrating its own uses, such as lambda class generation, onto it. The main limitation is that it only exists on JDK 24 and later, so I would use it for new tools and port existing ASM code only when we no longer support older JDKs.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25, Java 24, Java 17, Java 21
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is the java.lang.classfile Class-File API, finalised in Java 24, and why did the...": https://scrivio-interview-library.netlify.app/java/reflection-and-method-handles/cc01818cac/