# 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 :

![](/files/-Lcz6VK2RXK22nkN1Pi1)

![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](/files/-Lcz6XwUYn90ihFNCuJq)

### 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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gyansetu-core-java-for-java.gitbook.io/project/3-inner-classes.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
