Synthetic Class in Java
Asked Answered
I

13

156

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

Internode answered 30/12, 2008 at 4:39 Comment(2)
All the "not in your code" answers are out of date with Java 8, as lambdas can be implemented as synthetic (not anonymous) classes.Dorthadorthea
To be fair, a lambda is still not "strictly" a class defined explicitly within your code. So, "not in your code" is still valid. The compiler generates the synthetic classes for you without explicit definition in your code.Twelve
N
16

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.

Nidia answered 31/12, 2008 at 4:52 Comment(4)
Yes, sample code would be good if you could provide an example/Gardant
This doesn't answer the question.Pindaric
For the switch case, I am not sure if this happens for every switch, but I've observed this for switch with enums; the compiler generates anonymous class with single static field that provides a mapping Enum.ordinal() -> 1, 2, 3... (so a sequence without gaps), and then a lookupswitch instruction runs the switch on this sequence, not directly on ordinals.Stature
This doesn't explain anything.Savage
F
118

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.

Filly answered 30/12, 2008 at 10:58 Comment(3)
Dynamic Proxies are not Synthetic Classes.Proof: Proxy.newProxyInstance(Runnable.class.getClassLoader(), new Class[]{Runnable.class}, (proxy, method, args1) -> null).getClass().isSynthetic() == falseKayak
The javadoc for java.lang.reflect.Member#isSynthetic says : Returns true if this member was introduced by the compiler; returns false otherwise.Triboluminescent
I believe that the javadoc for java.lang.reflect.Member#isSynthetic is irrelevant to the original question. Members are fields, constructors and methods. The original question was about synthetic classes, not synthetic members. In Java 8, lambda expressions give rise to synthetic classes - I'm not sure what other circumstances they can arise.Shahaptian
I
58

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

Internode answered 30/12, 2008 at 4:49 Comment(0)
N
16

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.

Nidia answered 31/12, 2008 at 4:52 Comment(4)
Yes, sample code would be good if you could provide an example/Gardant
This doesn't answer the question.Pindaric
For the switch case, I am not sure if this happens for every switch, but I've observed this for switch with enums; the compiler generates anonymous class with single static field that provides a mapping Enum.ordinal() -> 1, 2, 3... (so a sequence without gaps), and then a lookupswitch instruction runs the switch on this sequence, not directly on ordinals.Stature
This doesn't explain anything.Savage
T
15

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() {
    }
  }
}
Trull answered 11/8, 2009 at 13:52 Comment(1)
@CiroSantilli烏坎事件2016六四事件法轮功, no, only synthetic accessor methods.Kayak
V
8

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.

Verger answered 30/12, 2008 at 13:22 Comment(2)
@OrangeDog: yes, that's what I wrote.Verger
This answer seems to be outdated since java 8 - lambdas and method references make use of this feature. I've added an answer demonstrating thatPhoenician
P
6

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/

Purchasable answered 7/8, 2014 at 7:5 Comment(2)
I guess you mean at compile time, and not at runtime.Sandi
@sathis, did you mean javac, not JVM?Kayak
P
5

As various answers have already pointed out, the compiler is allowed to generate various constructs (including classes) that do not directly correspond to something in souce code. These have to be marked as synthetic:

13.1. The Form of a Binary

A binary representation for a class or interface must also contain all of the following:
[...]
11. A construct emitted by a Java compiler must be marked as synthetic if it does not correspond to a construct declared explicitly or implicitly in source code, unless the emitted construct is a class initialization method (JVMS §2.9).
[...]

As pointed out by @Holger in a comment to another question, relevant examples for such constructs are the Class objects representing method references and lambdas:

System.out.println(((Runnable) System.out::println).getClass().isSynthetic());
System.out.println(((Runnable) () -> {}).getClass().isSynthetic());

Output:

true
true

While this is not explicitly mentioned, it follows from 15.27.4. Run-Time Evaluation of Lambda Expressions:

The value of a lambda expression is a reference to an instance of a class with the following properties: [...]

and the almost identical wording for method references (15.13.3. Run-Time Evaluation of Method References).

As this class is not explicitly mentioned anywhere in source code, it has to be synthetic.

Phoenician answered 22/11, 2018 at 10:53 Comment(1)
Why does this not have more upvotes!!?? Excellent, thorough answer!Mauk
C
4

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
Carob answered 23/10, 2019 at 6:26 Comment(1)
the article is very useful, thank youHeliochrome
H
3

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

http://www.easymock.org/

Haitian answered 30/12, 2008 at 13:4 Comment(0)
W
2

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

Wearisome answered 17/6, 2014 at 19:35 Comment(0)
O
1

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.

Otalgia answered 23/6, 2011 at 10:50 Comment(3)
i thought it was an anonymous inner classDara
I have to agree with @KumarAbhinav . Not all anonymous inner classes are synthetic. See: xinotes.net/notes/note/1339Tiertza
Anonymous inner classes don't generate synthetic classes on Oracle JDK 1.8.0_45, they generate separate non-synthetic classes with names of type Outer$1.class.Thor
A
1

A synthetic class does not appear in your code: it is made up by compiler. E.g. A bridge method made up by compiler in java is typically synthetic.

public class Pair<T> {
    private T first;
    private T second;
    public void setSecond(T newValue) {
        second = newValue;
    }
}


public class DateInterval extends Pair<String> {
    public void setSecond(String second) {
        System.out.println("OK sub");
    }

    public static void main(String[] args) throws NoSuchFieldException, SecurityException {
        DateInterval interval = new DateInterval();
        Pair pair = interval;
        pair.setSecond("string1");
    }
}

Using the command javap -verbose DateInterval, you can see a bridge method:

public void setSecond(java.lang.Object);
flags: ACC_PUBLIC, ACC_BRIDGE, ACC_SYNTHETIC

This was made up by compiler; it does not appear in your code.

Attainable answered 4/4, 2015 at 3:46 Comment(0)
L
0

Synthetic constructs are classes, methods, fields etc that do not have a corresponding construct in the source code. 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.

Leid answered 2/2, 2015 at 16:16 Comment(1)
-1. The very same text, verbatim, was posted in the answer given one year before yours [https://mcmap.net/q/65807/-synthetic-class-in-java], and further yet that answer additionally cited the official reference where the text originates, and provided formatting for easier reading. Please don't greedily spam questions with clearly duplicate answers.Costly

© 2022 - 2024 — McMap. All rights reserved.