- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is a Nested Class in Java?
A nested class is a class inside a class in Java. It is of two types i.e. Static nested class and Inner class. A static nested class is a nested class that is declared as static. An inner class is a non-static nested class.
A program that demonstrates a static nested class is given as follows:
Example
public class Class1 { static class Class2 { public void func() { System.out.println("This is a static nested class"); } } public static void main(String args[]) { Class1.Class2 obj = new Class1.Class2(); obj.func(); } }
Output
This is a static nested class
Now let us understand the above program.
The class Class1 is the outer class and the class Class2 is the static nested class. The method func() in Class2 prints "This is a static nested class". A code snippet which demonstrates this is as follows:
public class Class1 { static class Class2 { public void func() { System.out.println("This is a static nested class"); } } }
An object obj is declared in the method main() in Class1. Then func() is called. A code snippet which demonstrates this is as follows:
public static void main(String args[]) { Class1.Class2 obj = new Class1.Class2(); obj.func(); }
- Related Articles
- What is the class "class" in Java?
- What is a static class in Java?
- What is a singleton class in Java?
- What is a Throwable class in Java?
- What is a Locale class in Java?
- What is a Java class library?
- What is a Pair class in Java Tuples?
- What is a Septet class in Java Tuples?
- What is a Quintet class in Java Tuples?
- What is a Triplet class in Java Tuples?
- What are nested interfaces in Java?
- What is AbstractSequentialList class in Java?
- What is AbstractCollection class in Java?
- What is AbstractList Class in Java?
- What is Java String Class?

Advertisements