Class.forName()
attempts to resolve a class at runtime, using the default class loader. It may succeed, or it may fail, depending on whether the desired class is valid and within your classpath.
The .class
operator evaluates the class descriptor as a compile-time constant. It can still fail at runtime if the referenced class is not valid and present within the classpath, but it can be used in places where constant expressions are required (e.g., in annotation values), and it is generally more resilient with respect to refactoring. Note that the class descriptor that is captured depends on the compiler's inputs and classpath; with different compiler arguments, the same code may end up referencing two different classes.
In your use case, both should resolve to the same class. Your results are unexpected, and may indicate the presence of another class named A
within your classpath. I suspect the interface A
that you quoted in your question is actually located within a package, in which case you are passing the wrong descriptor to forName()
. The name should be package-qualified.