# 3) Inner Classes

A nested class is a class defined within another class. Nested classes that are declared as static are referred to as static nested classes. Nested classes that aren’t declared as static are referred to as inner classes. Like a regular top-level class, an inner or static nested class can define variables and methods.

### Create top-level and nested classes :

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-Lcz5lsy46Zoxi_HUKv5%2F-Lcz6VK2RXK22nkN1Pi1%2FScreen%20Shot%202019-04-21%20at%202.25.14%20PM.png?alt=media\&token=fcd54f32-fde9-4831-a368-e6c42913acb7)

![An outer class showing all types of inner classes that it can define: inner class, static nested class, local inner class, and anonymous inner class](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-Lcz5lsy46Zoxi_HUKv5%2F-Lcz6XwUYn90ihFNCuJq%2FScreen%20Shot%202019-04-21%20at%202.25.45%20PM.png?alt=media\&token=666fdc54-9474-47fe-bcd1-8e1e9629ef2a)

### Advantages of inner classes :

1. Objectify the functionality of a class, within it.For example, you might define a class Tree, which defines operations to add objects, remove objects, and sort them based on a condition. Instead of defining methods and variables to sort them within the class Tree, you could encapsulate sorting functionality within another class TreeSort. Because the class TreeSort would always work with Tree and might not be needed outside the class Tree, TreeSort can be defined as an inner class within class Tree.
2. For using inner classes is as parameter containers.Instead of using long method signa- tures, inner classes are often used to keep method signatures compact by passing ref- erence parameters of inner classes instead of a long list of individual parameters.



