Uninteresting Anonymous Classes in Java

About 6 years have passed since I wrote Java, so please excuse the rust.

I am working with a library method that requires passing Class objects. Since I will have to call this method a dynamic number of times, each time with a slightly different Class argument, I would like to pass an anonymous class to it.

However, in all the documents / textbooks that I could find, so far it is only about creating instances of anonymous classes, for example :

 new className(optional argument list){classBody} new interfaceName(){classBody} 

Is it possible to define an anonymous class without creating it? Or perhaps more clearly, can I create a Class object for an anonymous class?

+4
source share
6 answers

Unfortunately, you cannot shy away from creating here. However, you can do this non-op:

 foo((new Object() { ... }).getClass()); 

Of course, this may not be an option if you need to extract from some class that performs some actions in the constructor.

EDIT

Your question also says that you want to call foo "every time with a slightly different class argument." The above will not do this, because there will still be one definition of an anonymous inner class, even if you put a new expression in a loop. That way, you're really not going to buy anything compared to the class name. In particular, if you try to do this to capture the values โ€‹โ€‹of some local variables, a new instance of your anonymous class that foo will be created using the Class object passed to it will not be captured by it.

+2
source

short answer

you cannot (using only JDK classes)

long answer

try:

 public interface Constant { int value(); } public static Class<? extends Constant> classBuilder(final int value) { return new Constant() { @Override public int value() { return value; } @Override public String toString() { return String.valueOf(value); } }.getClass(); } 

create two new classes of "parametric" classes:

 Class<? extends Constant> oneClass = createConstantClass(1); Class<? extends Constant> twoClass = createConstantClass(2); 

however, you cannot instantiate these classes:

 Constant one = oneClass.newInstance(); // <--- throws InstantiationException Constant two = twoClass.newInstance(); // <--- ditto 

it will fail at runtime since there is only one instance for each anonymous class .

However, you can create dynamic classes at runtime using bytecode manipulation libraries such as ASM . Another approach is to use dynamic proxies , but this approach is a drawback, which allows you to proxy only interface methods (so you need a Java interface).

+1
source

You can only refer to the anonymous ONCE class. If you do not create it, you cannot create an instance, since you do not have a name for it.

Therefore, I believe that anonymous classes can only be used in conjunction with the โ€œnew BaseClass ()โ€.

In your situation, you will pass the BaseClass object to your method that does the work, and create an anonymous object in the source code when you need to pass the object.

0
source

You cannot access a class object of an anonymous class without setting it. However, if you only need access to the class, you can define local classes in your method and access them using the ClassName.class literal syntax.

0
source

You can take the name of the anonymous class and call Class.forName("mypackage.MyBaseClass$1") to get the handle of the anonymous class. This will give you the first anonymous class defined in your MyBaseClass , so this is a pretty fragile way to access the class.

I suspect that everything you are trying to do can be done better. What are you really trying to achieve? Perhaps we can suggest a method that does not require you to pass Class this way.

0
source

You can access the class object of an anonymous class by calling it .getClass() immediately after creation. But what good will it do?

I think the key is in this part of what you said:

I am working with a library method that requires me to pass a class of objects to it.

Why does he want to pass you class objects? What does this library do with class objects that you pass? Create objects? But if so, which constructor does he use and how does he decide which arguments should pass? I donโ€™t know which library you use or what it does, but I would assume that it always creates objects using the constructor with no arguments. However, this will not work for anonymous classes, since they do not have an open constructor (and in any case, an external instance must be referenced to create an instance of a non-static inner class, so there is no constructor without arguments).

0
source

All Articles