Java local class with examples

0
2394

1. Java local class :

Sometimes we can declare a class inside any block such as instance block or constructor or method or if block, such type of inner classes are called local inner classes or local classes. You can define a java local class inside any block. For example, you can define a local class in a method body, a for loop, or an if clause.

The main purpose of local inner class is to define logic that required instantly to use in local scope.

class Outer {
    
    // a Class inside Constructor
    Outer(){
        
         class ConstructorLocal {
             
         }
    }
    
    // a class inside instance block
    {
        class Local{
            
        }
    }
    
    // a class inside static block
    static {
        class StaticLocal{
            
        }
    }
    
    // a class inside method
    public void add(int a, int b) {
        
        class MethodLocal {
            
        }
        
        // a class inside if block
        if(a > b) {
            
            class Local{
                
            }
        }
    }    
}

2. Scope of java Local classes

We can access local class only within the method or block where we declared it. That is from outside of the block we can’t access. As the scope of local inner classes is very less, this type of inner classes are most rarely used type of inner classes. Mostly this kind of use cases we can see in multi threading programming.

2.1. Example

class Outer {
    
    public void methodOne() {
        
        class Inner{
            
            public void sum(int i,int j) {
                System.out.println("The sum:"+(i+j));
            }
        }
        
        // out side of this method Local Inner class can't be accessed
        Inner i = new Inner();
        i.sum(10,20);
        i.sum(100,200);
    }
    
    public static void main(String[] args) {
        //Inner i = new Inner(); can not access
        //new Outer().new Inner(); can not access
        new Outer().methodOne();
    }
}

Output :

The sum:30
The sum:300

3. Accessing members in local class

1. If we are declaring java local class inside instance block or method then we can access both static and non static members of outer class directly.
2. But if we are declaring inner class inside static method or block then we can access only static members of outer class directly and we can’t access instance members directly.
3. We can access constants directly inside local classes and we can declare also. A final variable is a constant, so that we can access final members directly.
4. Only allowed modifiers for local classes are final, abstract and strictfp.

3.1. Example

class Outer{
    
    int x = 10;
    static int y = 20;
    static final int z = 30;
    
    static {
        // Defining class in static area
        class Inner {
            
            static int m = 40;// Cant declare static members in non static inner type
            static final int n = 50;
            
            public void methodTwo() {
                System.out.println(x);// can not access instance members
                System.out.println(y);//20
                System.out.println(z);//30
            }
        }
    }
    
    public void methodOne() {
        
        class Inner {
            static int m = 40;// Cant declare static members in non static inner type
            static final int n = 50;
 
            public void methodTwo() {
                System.out.println(x);//10
                System.out.println(y);//20
                System.out.println(n);//50
            }
        }
        
        Inner i=new Inner();
        i.methodTwo();
    }
    
    public static void main(String[] args) {
        new Outer().methodOne();
    }
}

Output :

You get compilation errors at line 11,15 and 25. Comment those lines and run the program, you get following output.

10
20
50

4. Shadowing and java local class

If a declaration of a type such as a member variable or a parameter name in a particular scope has the same name as another declaration in the enclosing scope, then the declaration shadows the declaration of the enclosing scope. You cannot refer to a shadowed declaration by its name alone. The following example demonstrates this.

public class LocalClassShadowingExample {

	int x = 10;
    int y = 20;
    static final int z = 30;
    
    public void methodOne() {
        
        class Inner {
            //static int m = 40;// Cant declare static members in non static inner type
        	int x = 90;
            static final int z = 100;
 
            public void methodTwo(int x) {
                System.out.println("x=> "+x);//111
                System.out.println("y=> "+y);//20
                System.out.println("z=> "+z);//100
                System.out.println("this.x=> "+this.x);//90
                System.out.println("LocalClassShadowingExample.this.x=> "+LocalClassShadowingExample.this.x);
                System.out.println("LocalClassShadowingExample.z=> "+LocalClassShadowingExample.z);
            }
        }
        
        Inner i=new Inner();
        i.methodTwo(111);
    }
    
    public static void main(String[] args) {
        new LocalClassShadowingExample().methodOne();
    }
}

Output :

x=> 111
y=> 20
z=> 100
this.x=> 90
LocalClassShadowingExample.this.x=> 10
LocalClassShadowingExample.z=> 30

References

  1. Java documentation

LEAVE A REPLY

Please enter your comment!
Please enter your name here