# 3.4) Method local inner classes

The method local inner classes are defined within static or instance methods of a class. Though these classes can also be defined within code blocks, they are typically created within methods.

Figure below shows the definition of a bare-bones method local inner class, Inner, defined within method outerMethod() of class Outer. The Java compiler generates separate class files for the classes Outer and Inner. Because a class can define method local inner classes with the same name in separate methods, the Java compiler adds a number to the name of the compiled file for the local inner classes.

![Compiler generates separate class files for an outer class and method local inner class. The name of the method local inner class is prefixed with the name Outer class, a $ sign, and an integer.](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-Lf7jgXTs0wiSiwFr__r%2F-Lf7lE1tUQXs-0D8fBU5%2FScreenshot%202019-05-18%20at%207.58.16%20AM.png?alt=media\&token=0f02e6c4-017c-43ce-a545-4d76a6ef637f)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-Lf7jgXTs0wiSiwFr__r%2F-Lf7lLj1gpyucvRwIp4V%2FScreenshot%202019-05-18%20at%207.58.49%20AM.png?alt=media\&token=f247caaf-4724-4dda-bc44-945aa08575b7)

For the preceding code, the Java compiler will generate three class files: Outer.class, Outer$1Inner.class, and Outer$2Inner.class.

### CHARACTERISTICS OF METHOD LOCAL INNER CLASSES:

Recall that none of the variables within a method can be defined using any explicit modifier (public, protected, private). Similarly, method local inner classes can’t be defined using any explicit access modifier. But a method local inner class can define its own constructors, variables, and methods by using any of the four access levels:

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-Lf7jgXTs0wiSiwFr__r%2F-Lf7loKBMb0sBfA5tCXX%2FScreenshot%202019-05-18%20at%207.58.49%20AM.png?alt=media\&token=29b019a7-919b-4f5d-b613-fa9a6e00feef)

### CREATION OF A LOCAL INNER CLASS:

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-Lf7jgXTs0wiSiwFr__r%2F-Lf7lvrL1bW3XebU8hBB%2FScreenshot%202019-05-18%20at%208.01.25%20AM.png?alt=media\&token=6340baa7-82fd-492c-8f1c-b1c91937a64b)

### WHAT CAN A METHOD LOCAL INNER CLASS ACCESS?:

A method local inner class can access all variables and methods of its outer class, including its private members and the ones that it inherits from its base classes. A method local inner class can also define members with the same name as its outer class. In this case, the members of the outer class can be referred to by using the name of the outer class followed by the implicit reference this. Class Inner can access members of class Outer by using Outer.this, as shown in the following code:

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-Lf7vwgPxwtsrzAlCTPm%2F-Lf7w1Gn4wS-4yu6DWE9%2FScreenshot%202019-05-18%20at%208.45.16%20AM.png?alt=media\&token=0c3c2cd1-5913-450f-87aa-8516948aca0c)

### Disadvantages of inner classes:

Using inner classes is an advanced concept, and it can be difficult for inexperienced programmers to identify, implement, and maintain them.
