What is a synthetic class in Java? Why should it be used? How can I use it?
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.