Synthetic Class in Java

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

    synthetic classes / methods / fields:

    These things are important for the VM. Have a look at following code snippet:

    class MyOuter {
    
      private MyInner inner;
    
      void createInner() {
        // The Compiler has to create a synthetic method
        // to construct a new MyInner because the constructor
        // is private.
        // --> synthetic "constructor" method
        inner = new MyInner();
    
        // The Compiler has to create a synthetic method
        // to doSomething on MyInner object because this
        // method is private.
        // --> synthetic "doSomething" method
        inner.doSomething();
      }
    
      private class MyInner {
        // the inner class holds a syntetic ref_pointer to
        // the outer "parent" class
        // --> synthetic field
        private MyInner() {
        }
        private void doSomething() {
        }
      }
    }
    

提交回复
热议问题