Quick revision
The short answer
It means two different class loaders each defined their own com.acme.Order, and the JVM treats them as two unrelated types, because a class is identified by its name plus its defining loader. Typical sources are Spring Boot DevTools' restart loader, application servers with a loader per application, plugin systems and OSGi. We find the cause by printing the class loader of the object and of the Order class at the failure point and by running with -Xlog:class+load to see where each copy came from, and we fix it by loading shared types from one common parent loader or removing the duplicate jar.
What to remember
In this answer, we saw that a class is identified by its name plus its defining loader, so two loaders that each define com.acme.Order create two distinct types and casts between them fail. We looked at the common sources, DevTools restarts, application servers, plugin systems and OSGi, found the loaders by printing them and by class load logging, and fixed the problem by loading shared types from one common parent or removing the duplicate.
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.
It means two different class loaders each defined their own com.acme.Order, and the JVM treats them as two unrelated types, because a class is identified by its name plus its defining loader. Typical sources are Spring Boot DevTools' restart loader, application servers with a loader per application, plugin systems and OSGi. We find the cause by printing the class loader of the object and of the Order class at the failure point and by running with -Xlog:class+load to see where each copy came from, and we fix it by loading shared types from one common parent loader or removing the duplicate jar.
Why this question is asked
Few error messages look as absurd as Order cannot be cast to Order. The code is correct, the names are identical, and casting an object to its own class should never fail. Interviewers ask this because the explanation reveals how the JVM really identifies classes, and because the same root cause shows up in DevTools, application servers, caches and plugin systems. In this answer, we will recap what makes two classes the same, reproduce the problem, see where it usually comes from, find the two loaders, and fix it.
1A Quick Recap: What Makes Two Classes the Same
A class loader turns class file bytes into a Class object. The loader that actually does this is called the defining loader. The JVM identifies a class at run time by two things together: its binary name, such as com.acme.Order, and its defining loader. So if loader A and loader B each read the same Order.class file and define it, the program has two distinct classes. They have the same name, the same fields and the same methods, but the JVM considers them as unrelated as Order and Invoice.
A cast checks whether the object's class is the target class or a subtype of it. An object created from loader A's Order is not an instance of loader B's Order, so the cast fails.
2Reproducing the Problem
We can reproduce it in a few lines with two independent URLClassLoader instances pointing at the same jar.
| Class from loader A | Class from loader B | |
|---|---|---|
| Binary name | com.acme.Order | com.acme.Order |
| Defining loader | URLClassLoader @1b6d3586 | URLClassLoader @4554617c |
| Class object | orderA | orderB, and orderA != orderB |
| Cast an object created by A to it | succeeds | ClassCastException |
Notice the second half of the message. Since JDK 11, the ClassCastException message names the module and the loader of each class, so the message itself often tells us which two loaders are involved. Older logs or wrapped exceptions may show only the short form, which is where the confusion starts.
3Where It Usually Comes From
In real applications nobody creates loaders by hand, but frameworks do. Spring Boot DevTools is the most common source in development. To restart quickly, it loads our project's own classes in a restart class loader and the dependency jars in a base loader that stays alive across restarts. If a library in the base loader keeps objects across a restart, such as a cache, or deserializes objects using its own loader, it can hand us an Order from the old restart loader or from the base loader, and our code, loaded by the new restart loader, cannot cast it.
Application servers give each deployed application its own loader. If the same jar is deployed in two applications, or once in the server's shared lib and once in the application, objects passed between them through a shared cache, a JNDI lookup or a static registry carry the wrong class. Plugin systems and OSGi create one loader per plugin or bundle by design, so any type exchanged between plugins must come from a shared parent. Even a single test setup can do it, if a custom loader loads test fixtures.
4Finding the Two Loaders
At the failure point, we print the loader of the object we received and the loader of the class we are casting to. If the exception message already names them, this confirms it; if not, it is the decisive step.
- Read the full exception messageJDK 11+ names both loaders
- Log both classes' loaders at the failure point
value.getClass()andOrder.class - Same loader type, different instances?YesObject survived a restart or redeployNoTwo loaders share the type
- Run with -Xlog:class+loadwhere did each copy come from?
- Load the shared type once, from a common parentor remove the duplicate jar
5 steps in order. Play them, or step through them one at a time.
Then we find where each copy was loaded from. Running with -Xlog:class+load=info, or -verbose:class on any JDK, logs every class as it loads with its source, so two lines for com.acme.Order with different sources or at different times tell the story: two jars, or the same location loaded again after a restart. On a server, listing every jar that contains com/acme/Order.class across the application and the server's shared directories finds duplicates.
5Fixing It
The rule behind every fix is the same: a type that crosses a loader boundary must be defined by one loader that both sides share, which means a common parent. On an application server, we put the shared API jar in one place only: either the server's shared library, used by all applications, or inside each application, but then never pass objects between applications directly. In a plugin system, the types exchanged between host and plugins live in an API jar loaded by the host's loader, and plugins must not bundle their own copy.
For DevTools, we either stop holding objects across restarts, for example by clearing caches on restart or turning off the cache in development, or tell DevTools to load the jar with the shared types in the restart loader as well, through a META-INF/spring-devtools.properties entry such as restart.include.mylib=/mylib-[\\w.-]+\\.jar; check the DevTools documentation for your version. Where the duplicate is simply an extra jar on the class path, we remove it. And where objects must cross boundaries without a shared type, we pass them in a neutral form, such as JSON or interfaces from the shared parent.
6Follow-up questions
Why can a LinkageError about loader constraints appear instead of a ClassCastException?
When a class from one loader calls a method on a class from another loader, and the method's signature mentions a type such as Order, the JVM records a loader constraint: both loaders must agree on which Order that name means. If they would resolve it to different classes, the JVM refuses to link the call and throws LinkageError with a message about a loader constraint violation. This catches the mismatch earlier, at link time, before any object of the wrong type can be passed, so it shows up instead of a ClassCastException for the same underlying problem.
Does serialization help, since it recreates the object?
It can, but it can also cause the problem. Deserialization creates a new object using some loader's class, and if the library doing it uses its own loader instead of the application's, the new object belongs to the wrong class. Good libraries let us pass the class loader to use; with DevTools, caches that serialize values sometimes need that configured.
Is this a production issue or only a development issue?
Both. DevTools runs only in development, so its version of the problem never reaches production. Application servers, plugin systems and frameworks that load modules dynamically produce the same error in production, often after a redeploy that leaves old objects alive in a shared cache.
7Conclusion
In this answer, we saw that a class is identified by its name plus its defining loader, so two loaders that each define com.acme.Order create two distinct types and casts between them fail. We looked at the common sources, DevTools restarts, application servers, plugin systems and OSGi, found the loaders by printing them and by class load logging, and fixed the problem by loading shared types from one common parent or removing the duplicate.
Open with
The names are the same but the classes are not. The JVM identifies a class by its binary name together with the loader that defined it, so if two class loaders each load com.acme.Order, we get two different runtime types, and an object of one cannot be cast to the other.
Then, if they want more
The modern exception message actually says this: since JDK 11 it names the loader of each side. The usual sources are frameworks that create loaders.
In development, Spring Boot DevTools loads our classes in a restart loader and dependencies in a base loader, so a cache or a deserializer in the base loader can hand back an Order from an old restart loader.
On application servers, each application has its own loader, so the same jar in two places or in the shared lib causes it, and plugin systems and OSGi do it by design.
To find it, I would log value.getClass().getClassLoader() and Order.class.getClassLoader() at the failure point, which shows the two loaders, and run with -Xlog:class+load or -verbose:class to see where each copy was loaded from.
The fix is to make the shared type come from one common parent loader: put the API jar in one place, keep plugins from bundling their own copy, stop caching objects across DevTools restarts or include that jar in the restart loader, or simply remove the duplicate jar.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 11
- 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.
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 "A service logs java.lang.ClassCastException: com.acme.Order cannot be cast to...": https://scrivio-interview-library.netlify.app/java/class-loading/f343942140/