Home ProgrammingJava Java Local Nested Class

Java Local Nested Class

by Log Raj Bhatt
0 comment 664 views

Local Nested Class Introduction

We have previously discussed about normal inner class that is member nested class. In this post we are going to discuss about Local Nested Class in Java.

A class defined inside a block is Local Nested Class. Block here means a method, static block or initialiser block. This act almost same as an Local variable to method enclosing it. It is visible only inside the scope defined by its enclosing block. Local Nested Classes are least populer kind of nested classes in java. We use Method Local Nested Classes to achieve method specific repeated functionality.

Some points about Local Nested Classes

  • Local nested class is type of Nested class hence can access any member of the enclosing class, even the private ones.
  • It is visible only within method enclosing it, because of its limited scope.
  • We can define local Nested class inside static or non-static methods.
  • If we define class inside static method, then inner class can access only static member of the outer class, not the instance members.
  • These class cannot access any local variable except the final ones.
  • We cannot make be public, private, protected or static since it is similar to local variable
  • Local classes can not contain static fields and methods but it may contain a field which is static and final.
  • You can not define Interface inside methods, but we can define abstract classes.
  • It cannot have same name as its enclosing class.


Example consist of two classes. One is that contain Local Nested Class.

package packku;

public class DemoLocal {
	double x=12;
	static String s;
	public void myMethod() {
		final int x=120;
		class LocalNested{
			void print() {
		new LocalNested().print();


and Another is main class for the project.


package packku;

public class InnerClassesMain {

	public static void main(String[] args) {
		new DemoLocal().myMethod();



As expected output will be


Local Nested Class Output

Local Nested Class Output

If you have any confusion comment down below. Happy Learning, Bye!

0 comment

Leave a Reply

Do NOT follow this link or you will be banned from the site!