Quick revision
The short answer
Proxy.newProxyInstance(loader, interfaces, handler) generates a class at run time that implements the given interfaces and extends java.lang.reflect.Proxy, and every method call on an instance goes to the handler's invoke(proxy, method, args). Because the generated class already extends Proxy, JDK proxies only work for interfaces and cannot proxy a concrete class. Subclass proxies from CGLIB or ByteBuddy can proxy classes, but they cannot intercept final methods or final classes, and equals, hashCode and toString also go through the handler and must be handled.
What to remember
In this answer, we saw that Proxy.newProxyInstance generates a class at run time that extends Proxy and implements the given interfaces, routing every call, including equals, hashCode and toString, to InvocationHandler.invoke. Because the generated class already has a superclass, JDK proxies only work for interfaces. CGLIB and ByteBuddy generate subclasses instead, which can proxy classes but cannot intercept final classes, final methods, private methods or static methods.
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.
Proxy.newProxyInstance(loader, interfaces, handler) generates a class at run time that implements the given interfaces and extends java.lang.reflect.Proxy, and every method call on an instance goes to the handler's invoke(proxy, method, args). Because the generated class already extends Proxy, JDK proxies only work for interfaces and cannot proxy a concrete class. Subclass proxies from CGLIB or ByteBuddy can proxy classes, but they cannot intercept final methods or final classes, and equals, hashCode and toString also go through the handler and must be handled.
Background to this question
Dynamic proxies are how much of the Java ecosystem adds behaviour around our code without us writing wrapper classes: Spring's transactions and security, Spring Data repositories that have no implementation, Feign and Retrofit HTTP clients, and Mockito mocks. Knowing how they are built explains a lot of everyday surprises, such as annotations that do nothing on final methods. In this answer, we will recap what a proxy is, build a JDK proxy and see what class it creates, follow a call into the handler, look at why only interfaces work, and compare with subclass proxies.
1A Quick Recap of the Proxy Idea
A proxy is an object that stands in for another object. It has the same type as the real one, so callers cannot tell the difference, but every call passes through the proxy first, which can log it, time it, check permissions, start a transaction or forward it somewhere else entirely. Writing proxies by hand means one wrapper class per interface. A dynamic proxy is generated at run time instead, for any interface, with one piece of code, the invocation handler, deciding what happens on every call.
2Creating a JDK Proxy
The JDK's mechanism lives in java.lang.reflect. We give Proxy.newProxyInstance three things: a class loader to define the generated class in, the interfaces it must implement, and an InvocationHandler.
6 messages in order. Play them, or step through them one at a time.
Notice what the JDK generated: a new class, named like $Proxy0, that extends java.lang.reflect.Proxy and implements Greeter. For each interface method it has a body that packs the arguments into an Object array and calls the handler's invoke method with the proxy itself, the Method object for greet, and the arguments. The generated class is created once per combination of interfaces and class loader and then reused.
3The Object Methods Go Through the Handler Too
A proxy also routes three methods of Object to the handler: equals, hashCode and toString. That surprises people, because a handler written only with the interface in mind may forward them in odd ways or break them. Other Object methods, such as getClass, wait and notify, are final and are not routed.
So a robust handler checks method.getDeclaringClass() == Object.class and handles those methods itself, for example comparing proxies by identity for equals and using System.identityHashCode, or forwarding to the real object when that is the intended meaning. Putting a broken proxy in a HashSet fails immediately, which is how this bug usually shows up.
4Why JDK Proxies Only Work for Interfaces
The generated class must have the same type as the object it replaces. For an interface that is easy: implement it. For a concrete class, the proxy would have to be a subclass of it, but the generated class already extends java.lang.reflect.Proxy, and Java allows only one superclass. So the API simply refuses classes.
That is the main limit of JDK proxies: code that is typed against the concrete class, or a class that implements no interface, cannot be proxied this way.
5Subclass Proxies With CGLIB or ByteBuddy
Bytecode libraries remove that limit by generating a subclass of the target class at run time and overriding its methods to call an interceptor. CGLIB, a copy of which Spring repackages and still uses for class-based proxies, and ByteBuddy, which Mockito and Hibernate use, both work this way. Because the proxy is a subclass, it can stand in for any non-final class.
The same mechanism brings its own limits. A final class cannot be subclassed, so it cannot be proxied at all. A final method cannot be overridden, so calls to it go straight to the original code and the interceptor never runs, silently. Private methods are not visible to the subclass, and static methods belong to the class rather than the object, so neither can be intercepted. Creating the subclass instance also normally runs a constructor, so libraries either need an accessible constructor or use a trick such as Objenesis to create instances without one.
| Question | JDK Proxy | CGLIB or ByteBuddy subclass |
|---|---|---|
| Can proxy an interface? | Yes | Yes |
| Can proxy a concrete class? | No: not an interface | Yes, if the class is not final |
| Intercepts final methods? | Not applicable | No: calls go to the original |
| Intercepts private or static methods? | Not applicable | No |
| Needs extra library? | No, part of the JDK | Yes, a bytecode library |
Mockito is the exception that proves the rule: its inline mock maker, the default since Mockito 5, does not subclass. It uses a Java agent to change the bytecode of the real class, which lets it mock final classes and methods, at the cost of relying on instrumentation.
6Follow-up questions
Why does Spring sometimes create a JDK proxy and sometimes a CGLIB one?
Spring AOP uses a JDK proxy when the bean implements at least one interface and class-based proxying is not requested, and a CGLIB subclass otherwise. Spring Boot sets spring.aop.proxy-target-class to true by default, so Boot applications get CGLIB proxies even for beans with interfaces, which avoids failures when someone injects the concrete class. The visible consequence is that with CGLIB, final methods on a @Transactional bean silently skip the transaction.
Can a JDK proxy implement several interfaces at once?
Yes. We pass all of them in the interfaces array, and the generated class implements each one. If two interfaces declare the same method signature, the handler receives the Method object of the first interface in the array that declares it.
How does a handler call a default method of the interface?
Calling method.invoke(proxy, args) would loop back into the handler. Since Java 16, InvocationHandler.invokeDefault(proxy, method, args) calls the interface's default implementation directly, which is the supported way to let default methods run normally.
7Conclusion
In this answer, we saw that Proxy.newProxyInstance generates a class at run time that extends Proxy and implements the given interfaces, routing every call, including equals, hashCode and toString, to InvocationHandler.invoke. Because the generated class already has a superclass, JDK proxies only work for interfaces. CGLIB and ByteBuddy generate subclasses instead, which can proxy classes but cannot intercept final classes, final methods, private methods or static methods.
Open with
Proxy.newProxyInstance takes a class loader, an array of interfaces and an InvocationHandler. At run time the JDK generates a class, named like $Proxy0, that extends java.lang.reflect.Proxy and implements those interfaces, and each method in it just calls handler.invoke with the proxy, the Method object and the arguments as an array.
Then, if they want more
The handler decides what happens, typically doing something before and after and forwarding to the real object with method.invoke. The generated class is cached per loader and interface set. The main limit is that it only works for interfaces.
Since the generated class already extends Proxy and Java has single inheritance, it can't be a subclass of a concrete class; passing a class gives IllegalArgumentException, not an interface. Subclass proxies from CGLIB or ByteBuddy generate a subclass of the target and override its methods, so they can proxy classes.
But they can't proxy final classes, and they can't intercept final, private or static methods; calls to a final method just run the original code silently.
Another detail people miss: equals, hashCode and toString also go through the handler, so a handler that returns null for unknown methods breaks hashCode with a NullPointerException; I check for methods declared by Object and handle them explicitly. In Spring, that's why you get JDK proxies for interface-based beans unless proxyTargetClass is set, which Boot does by default.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 16
- 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 "How does java.lang.reflect.Proxy create a dynamic proxy? What are its limits compared...": https://scrivio-interview-library.netlify.app/java/reflection-and-method-handles/419f2499be/