
- Java 16 Tutorial
- Java 16 - Home
- Java 16 - Overview
- Java 16 - Environment Setup
- Java 16 Language Changes
- Java 16 - Sealed Classes
- Java 16 - Pattern Matching for instanceof
- Java 16 - Warnings for Value-Based Classes
- Java 16 - Record
- Java 16 JVM Changes
- Java 16 - Packaging Tools
- Java 16 - Garbage Collectors
- Java 16 - Other Changes
- Java 16 - Deprecation & Removals
- Java Other Versions Tutorials
- Java Tutorial
- Java 8 Tutorial
- Java 9 Tutorial
- Java 10 Tutorial
- Java 11 Tutorial
- Java 12 Tutorial
- Java 13 Tutorial
- Java 14 Tutorial
- Java 15 Tutorial
- Java 16 Useful Resources
- Java 16 - Quick Guide
- Java 16 - Useful Resources
- Java 16 - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java 16 - Warning for Value-Based Classes
Some classes, such as java.util.Optional and java.time.LocalDateTime, are value-based. Such Instances of a value-based class are final and immutable. Such classes have annotation @jdk.internal.ValueBased and Java 16 now generates compile time warnings in case such classes are synchronized using synchronized keyword. Wrapper classes are value based. For example, Double class is a value based.
Example
package java.lang; @jdk.internal.ValueBased public final class Double extends Number implements Comparable<Double>, Constable, ConstantDesc { //... }
Consider the following example:
ApiTester.java
Example
public class APITester { public static void main(String[] args) { Double d = 10.0; synchronized (d) { System.out.println(d); } } }
Compile and Run the program
$javac APITester.java
Output
APITester.java:4: warning: [synchronization] attempt to synchronize on an instance of a value-based class synchronized (d) { ^ 1 warning
Advertisements