tags:

views:

3956

answers:

6

What is a Synthetic Class in Java? Why should it be used? and How can I use it?

+6  A: 

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...

andHapp
+14  A: 

Java has the ability to create classes at runtime. These classes are know 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.

Andrew Newdigate
+1  A: 

Also Synthetic Classes or Dynamic Proxies are used by EasyMock to create implementations of interfaces or abstract classes at runtime.

http://www.easymock.org/

damian
A: 

According to this discussion, though the language specification describes an "isSynthetic" proprty for classes, this is pretty much ignored by implementations and not used for either dynamic proxies or anonymous classes. Synthetic fields and constructors are used to implement nested classes (there is not concept of nested classes in byte code, only in source code).

I think that the concept of synthetic classes has simply proven to be not useful, i.e. nobody cares whether a class is synthetic. With fields and methods, it's probably used in exactly one place: to determine what to show in an IDE class structure view - you want normal methods and fields to show up there, but not the synthetic ones used to simulate nested classes. OTOH, you DO want anonymous classes to show up there.

Michael Borgwardt
+1  A: 

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 refelction 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 dont believe you are able to access(other than refecection) the synthetic classses.

Milhous
Interesting. Do you have some sample code for this?
OscarRyz
+4  A: 

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 "constructor" 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() {
    }
  }
}