Why are two class files created on compiling?

柔情痞子 提交于 2021-02-08 10:26:09

问题


I knew that when a class has an inner class then this class will be compiled to two class files. Today I have codes as below

public class GenericDeserializer {

    public static void main(String[] args) {
        String cityPageLoadJson = "{\"count\":2,\"pageLoad\":[{\"id\":4,\"name\":\"HAN\"},{\"id\":8,\"name\":\"SGN\"}]}";
        Type type = new TypeToken<GenericResult<City>>() {
        }.getType();
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        GenericResult<City> cityPageLoad = gson.fromJson(cityPageLoadJson, type);
        for (City city : cityPageLoad.getPageLoad()) {
            System.out.println(gson.toJson(city));
        }
    }

}

Although the above one has no inner class, java compiler still creates two class files:

GenericDeserializer.class
GenericDeserializer$1.class

Using Java Decompiler tool, I see content of the second

package net.tuandn.training.lesson.gson;

import com.google.gson.reflect.TypeToken;
import net.tuandn.training.model.City;
import net.tuandn.training.model.GenericResult;

final class GenericDeserializer$1 extends TypeToken<GenericResult<City>>
{
}

Could anybody please explain why this class is created?
And when are multiple class files created on compiling?

Thank a lot!


回答1:


new TypeToken<GenericResult<City>>() {
}

creates an anonymous inner class. Anonymous inner classes, just like inner classes are compiled to separate class files. Since anonymous class don't have name, that is why numbers are used to generate unique names for each such classes. The number you see there after $ is the numbering for that anonymous class, as they come in order in your enclosing class.

If you use more anonymous classes like that, the number will increment by 1. So for more anonymous classes, the generated class files would look like:

GenericDeserializer$1.class
GenericDeserializer$2.class
GenericDeserializer$3.class
GenericDeserializer$4.class
....

For inner classes however, the value after the $ is the name of the inner class, as you already would have noticed.

And when are multiple class files created on compiling?

Yes, those classes are generated, when you compile your top-level class.




回答2:


Two class files are generated because you are using an anonymous class in the following statement:

    TypeToken<GenericResult<City>>() {
    .....
    }

Each anonymous class file uses the same name as of the container class and appends a $1/$2 and so on.




回答3:


Simple enough, your decompiled class shows

final class GenericDeserializer$1 extends TypeToken<GenericResult<City>>

So you have a TypeToken<GenericResult<City>> somewhere.

Looking through your code we see

Type type = new TypeToken<GenericResult<City>>() { /* anonymous inner class */ }.getType();

There's an anonymous inner class declaration there which will therefore get its own class file with $X suffix.



来源:https://stackoverflow.com/questions/18642422/why-are-two-class-files-created-on-compiling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!