Local Inner Class in Java
Last Updated :
07 Dec, 2021
Prerequisites: Nested Classes in Java
Local Inner Classes are the inner classes that are defined inside a block. Generally, this block is a method body. Sometimes this block can be a for loop or an if clause. Local Inner classes are not a member of any enclosing classes. They belong to the block they are defined within, due to which local inner classes cannot have any access modifiers associated with them. However, they can be marked as final or abstract. This class has access to the fields of the class enclosing it. Local inner class must be instantiated in the block they are defined in.
Rules of Local Inner Class:
- The scope of the local inner class is restricted to the block they are defined in.
- A local inner class cannot be instantiated from outside the block where it is created in.
- Till JDK 7, the Local inner class can access only the final local variable of the enclosing block. However, From JDK 8, it is possible to access the non-final local variable of enclosing block in the local inner class.
- A local class has access to the members of its enclosing class.
- Local inner classes can extend an abstract class or implement an interface.

Declaring a Local Inner class: A local inner class can be declared within a block. This block can be either a method body, initialization block, for loop, or even an if statement.
Accessing Members: A local inner class has access to fields of the class enclosing it as well as the fields of the block that it is defined within. These classes, however, can access the variables or parameters of the block that encloses it only if they are declared as final or are effectively final. A variable whose value is not changed once initialized is called an effectively final variable. A local inner class defined inside a method body has access to its parameters.
What happens at compile time?
When a program containing a local inner class is compiled, the compiler generates two .class files, one for the outer class and the other for the inner class that has the reference to the outer class. The two files are named by the compiler as:
- Outer.class
- Outer$1Inner.class
Declaration within a method body
Java
public class Outer
{
private void getValue()
{
int sum = 20 ;
class Inner
{
public int divisor;
public int remainder;
public Inner()
{
divisor = 4 ;
remainder = sum%divisor;
}
private int getDivisor()
{
return divisor;
}
private int getRemainder()
{
return sum%divisor;
}
private int getQuotient()
{
System.out.println( "Inside inner class" );
return sum / divisor;
}
}
Inner inner = new Inner();
System.out.println( "Divisor = " + inner.getDivisor());
System.out.println( "Remainder = " + inner.getRemainder());
System.out.println( "Quotient = " + inner.getQuotient());
}
public static void main(String[] args)
{
Outer outer = new Outer();
outer.getValue();
}
}
|
Output
Divisor = 4
Remainder = 0
Inside inner class
Quotient = 5
Note: A local class can access local variables and parameters of the enclosing block that are effectively final.
For example, if you add the highlighted assignment statement in the Inner class constructor or any method of Inner class in the above example:
public Inner()
{
sum = 50;
divisor = 4;
remainder = sum%divisor;
}
Because of this assignment statement, the variable sum is not effectively final anymore. As a result, the Java compiler generates an error message similar to “local variables referenced from an inner class must be final or effectively final.”
Declaration inside an if statement
Java
public class Outer
{
public int data = 10 ;
public int getData()
{
return data;
}
public static void main(String[] args)
{
Outer outer = new Outer();
if (outer.getData() < 20 )
{
class Inner
{
public int getValue()
{
System.out.println( "Inside Inner class" );
return outer.data;
}
}
Inner inner = new Inner();
System.out.println(inner.getValue());
}
else
{
System.out.println( "Inside Outer class" );
}
}
}
|
Output
Inside Inner class
10
Demonstrating Erroneous codes for Inner class
Java
public class Outer
{
private int getValue( int data)
{
static class Inner
{
private int getData()
{
System.out.println( "Inside inner class" );
if (data < 10 )
{
return 5 ;
}
else
{
return 15 ;
}
}
}
Inner inner = new Inner();
return inner.getData();
}
public static void main(String[] args)
{
Outer outer = new Outer();
System.out.println(outer.getValue( 10 ));
}
}
|
Output
Compilation error
Explanation: The above program causes compilation error because the inner class cannot be declared static. Inner classes are associated with the block they are defined within and not with the external class(Outer in this case).
Java
public class Outer
{
private void myMethod()
{
class Inner
{
private void innerMethod()
{
System.out.println( "Inside inner class" );
}
}
}
public static void main(String[] args)
{
Outer outer = new Outer();
Inner inner = new Inner();
System.out.println(inner.innerMethod());
}
}
|
Output
prog.java:20: error: cannot find symbol
Inner inner = new Inner();
^
symbol: class Inner
location: class Outer
Explanation: The above program causes compilation error because the scope of inner classes is restricted to the block they are defined in.
Similar Reads
How to Access Inner Classes in Java?
In Java, inner class refers to the class that is declared inside class or interface which were mainly introduced, to sum up, same logically relatable classes as Java is purely object-oriented so bringing it closer to the real world. It is suggested to have adequate knowledge access the inner class,
3 min read
Anonymous Inner Class in Java
Nested Classes in Java is prerequisite required before adhering forward to grasp about anonymous Inner class. It is an inner class without a name and for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain "extras" such as o
7 min read
Java.lang.ThreadLocal Class in Java
This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable. Basically, it is another way to achieve thread safety apart from writing im
4 min read
Static class in Java
Java allows a class to be defined within another class. These are called Nested Classes. Classes can be static which most developers are aware of, henceforth some classes can be made static in Java. Java supports Static Instance Variables, Static Methods, Static Block, and Static Classes. The class
3 min read
Java - Inner Class vs Sub Class
Inner Class In Java, one can define a new class inside any other class. Such classes are known as Inner class. It is a non-static class, hence, it cannot define any static members in itself. Every instance has access to instance members of containing class. It is of three types: Nested Inner ClassMe
3 min read
java.time.LocalDateTime Class in Java
java.time.LocalDateTime class, introduced in Java 8, represents a local date-time object without timezone information. The LocalDateTime class in Java is an immutable date-time object that represents a date in the yyyy-MM-dd-HH-mm-ss.zzz format. It implements the ChronoLocalDateTime interface and in
4 min read
Java File Class
Java File class is a representation of a file or directory pathname. Because file and directory names have different formats on different platforms, a simple string is not adequate to name them. Java File class contains several methods for working with the pathname, deleting and renaming files, crea
6 min read
java.time.InstantSource Class in Java
The java.time.Instant class in Java basically represents a point in time. It is a precise moment on the timeline of the Unix epoch. The Unix epoch is the time 00:00:00 UTC on 1 January 1970. The Instant class can be used to represent timestamps, durations, and intervals. Instant values can be obtain
3 min read
Java.lang.Void Class in Java
Java.lang.Void class is a placeholder that holds a reference to a class object if it represents a void keyword. It is an uninstantiable placeholder. Well, uninstantiable means that this class has a private constructor and no other constructor that we can access from outside. Methods of lang.void cla
1 min read
Compiler Class in Java
Compiler Class provides support and related services to Java code to Native Code. Native code is a form of code that can be said to run in a virtual machine (for example, [JVM]Java Virtual Machine). Declaration: public final class Compiler extends ObjectMethods of Java Compiler Class 1. command() Th
2 min read