
- Java 11 Tutorial
- Java 11 - Home
- Java 11 - Overview
- Java 11 - Environment Setup
- Java 11 - Standard HttpClient
- Java 11 - Compile free Launch
- Java 11 - String APIs
- Java 11 - Collections to Array
- Java 11 - File APIs
- Java 11 - Optional Class
- Java 11 - Not Predicate
- Java 11 - var in lambda
- Java 11 - Nest Based Access
- Java 11 - Removed/Deprecated API
- Java Other Versions Tutorials
- Java Tutorial
- Java 8 Tutorial
- Java 9 Tutorial
- Java 10 Tutorial
- Java 12 Tutorial
- Java 13 Tutorial
- Java 14 Tutorial
- Java 15 Tutorial
- Java 16 Tutorial
- Java 11 Useful Resources
- Java 11 - Quick Guide
- Java 11 - Useful Resources
- Java 11 - 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 11 - Nested Based Access
Java 11 introduced a concept of nested class where we can declare a class within a class. This nesting of classes allows to logically group the classes to be used in one place, making them more readable and maintainable. Nested class can be of four types −
Static nested classes
Non-static nested classes
Local classes
Anonymous classes
Java 11 also provide the concept of nestmate to allow communication and verification of nested classes.
Consider the following example −
ApiTester.java
import java.util.Arrays; import java.util.Set; import java.util.stream.Collectors; public class APITester { public static void main(String[] args) { boolean isNestMate = APITester.class.isNestmateOf(APITester.Inner.class); boolean nestHost = APITester.Inner.class.getNestHost() == APITester.class; System.out.println(isNestMate); System.out.println(nestHost); Set<String> nestedMembers = Arrays.stream(APITester.Inner.class.getNestMembers()) .map(Class::getName) .collect(Collectors.toSet()); System.out.println(nestedMembers); } public class Inner{} }
Output
true true [APITester$Inner, APITester]
Advertisements