Synthetic Class in Java

后端 未结 13 1755
广开言路
广开言路 2020-11-29 17:15

What is a synthetic class in Java? Why should it be used? How can I use it?

13条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-29 17:36

    What is a synthetic class in Java?

    A synthetic class is a .class file generated by Java Compiler and it does not exist in the source code.

    Example usage of synthetic class: Anonymous inner class

    • java.text.DigitList$1 is a synthetic class and it is a anonymous inner class inside java.text.DigitList
    • And there is no source code file named like DigitList$1.java but it is a inner file in DigitList.java

    Why should it be used?

    It is a mechanism inside Java compiler logic to generate the .class file

    How can I use it?

    No, developers do NOT use it directly.

    Java compiler use synthetic to generate .class file, and then JVM reads the .class file to execute the program logic.

    More details

    • This article explains synthetic class in details
    • This link lists all synthetic class exits in JDK

提交回复
热议问题