Synthetic Class in Java

后端 未结 13 1731
广开言路
广开言路 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:26

    For example, When you have a switch statement, java creates a variable that starts with a $. If you want to see an example of this, peek into the java reflection of a class that has a switch statement in it. You will see these variables when you have at least one switch statement anywhere in the class.

    To answer your question, I don't believe you are able to access(other than reflection) the synthetic classes.

    If you are analyzing a class that you don't know anything about (via reflection) and need to know very specific and low-level things about that class, you may end up using Java reflection methods that have to do with synthetic classes. The only "use" here is get more information about the class in order to use it appropriately in your code.

    (If you're doing this, you're probably building a framework of some sorts that other developers could use. )

    Otherwise, if you are not using reflection, there are no practical uses of synthetic classes that I know of.

    0 讨论(0)
  • 2020-11-29 17:26

    Well I found the answer to the first question on google:

    A class may be marked as synthetic if it is generated by the compiler, that is, it does not appear in the source code.

    This is just a basic definition but I found it in a forum thread and there was no explanation. Still looking for a better one...

    0 讨论(0)
  • 2020-11-29 17:28

    Java has the ability to create classes at runtime. These classes are known as Synthetic Classes or Dynamic Proxies.

    See http://java.sun.com/j2se/1.5.0/docs/guide/reflection/proxy.html for more information.

    Other open-source libraries, such as CGLIB and ASM also allow you to generate synthetic classes, and are more powerful than the libraries provided with the JRE.

    Synthetic classes are used by AOP (Aspect Oriented Programming) libraries such as Spring AOP and AspectJ, as well as ORM libraries such as Hibernate.

    0 讨论(0)
  • 2020-11-29 17:28

    They are created by JVM at run time when they invoke private members of inner class for debugging purpose

    The methods,fields,class created by JVM during run time for its execution purpose are called Synthetic

    http://www.javaworld.com/article/2073578/java-s-synthetic-methods.html

    http://javapapers.com/core-java/java-synthetic-class-method-field/

    0 讨论(0)
  • 2020-11-29 17:31

    When the Java compiler compiles certain constructs, such as inner classes, it creates synthetic constructs; these are classes, methods, fields, and other constructs that do not have a corresponding construct in the source code.
    Uses: Synthetic constructs enable Java compilers to implement new Java language features without changes to the JVM. However, synthetic constructs can vary among different Java compiler implementations, which means that .class files can vary among different implementations as well.
    reference:docs.oracle.com

    0 讨论(0)
  • 2020-11-29 17:31

    If I get it right, a synthetic class is one generated on the fly, without having to give it an explicit name. For example:

    //...
    Thread myThread = new Thread() {
             public void run() {
               // do something ...
             }
           };
    myThread.start();
    //...
    

    This creates a synthetic subclass of Thread and overrides its run() method, then instantiates it and starts it.

    0 讨论(0)
提交回复
热议问题