Inner Class in JAVA

BansodThomas
4 min readApr 2, 2024

--

(You can learn about Static Nested Class here )

(You can learn about Non-Static Nested Class here )

3.Local Class

Definition: In Java, an inner class defined inside a method or a scope block is called a local class.

Declaration: Declared within a block of code.

Characteristics:

  1. Local Classes are visible only with in the block in which they are define.
  2. Can access both static & non-static members/variables of the outer class but these variables must be effectively final (i.e., they cannot be modified inside the local class).

What does effectively final means?

In Java, a variable is considered effectively final if its value is never changed after it is initially assigned. This means that the variable is effectively treated as if it were declared as final, even though the final keyword is not used.

example:

public class OuterClass {
private int outerField = 10;

public void localClassExample() {
int localVar = 20;

class LocalClass {
void display() {
System.out.println("Outer field: " + outerField);
System.out.println("Local variable: " + localVar);
}
}

LocalClass local = new LocalClass();
local.display();
}
}

Advantages:

  1. Encapsulation: To maintain code organization and modularity, local classes might include functionality that is exclusive to a certain method or section of code.

example:

public class OuterClass {
private int outerField = 10;

public void localClassExample() {
int localVar = 20;

class LocalClass {
void display() {
System.out.println("Outer field: " + outerField);
System.out.println("Local variable: " + localVar);
}
}

LocalClass local = new LocalClass();
local.display();
}

public static void main(String[] args) {
OuterClass outer = new OuterClass();
outer.localClassExample();
}
}

LocalClass is encapsulated within the localClassExample method. It can access the outerField of OuterClass and the localVar of the localClassExample method. This encapsulation keeps the class definition localized to where it is needed, improving the modularity and organization of the code.

2. Access to Enclosing Scope: They can utilize the local variables and arguments of the enclosing method, as well as the members of the enclosing class, to construct complicated logic.

example:

public class Outer {
private int outerField = 10;

public void outerMethod() {
int localVar = 20;

class LocalInner {
void innerMethod() {
System.out.println("Outer field: " + outerField);
System.out.println("Local variable: " + localVar);
}
}

LocalInner inner = new LocalInner();
inner.innerMethod();
}

public static void main(String[] args) {
Outer outer = new Outer();
outer.outerMethod();
}
}

LocalInner class is a local class defined inside the outerMethod of the Outer class. It can access both the outerField of the Outer class and the localVar of the outerMethod. When outerMethod is called, an instance of LocalInner is created and its innerMethod is invoked, printing the values of outerField and localVar.

3.Code Reusability: Local classes are defined inside a certain block or procedure, yet they are still reusable inside that scope. This saves you from having to write duplicate code when defining a class for a particular environment.

example:

public class MathOperations {
public void performCalculations() {
int num1 = 10;
int num2 = 5;

// Local class for performing arithmetic operations
class Arithmetic {
public int add() {
return num1 + num2;
}

public int subtract() {
return num1 - num2;
}
}

// Using the local class to perform calculations
Arithmetic arithmetic = new Arithmetic();
System.out.println("Addition: " + arithmetic.add());
System.out.println("Subtraction: " + arithmetic.subtract());
}

public static void main(String[] args) {
MathOperations math = new MathOperations();
math.performCalculations();
}
}

Arithmetic class is a local class defined within the performCalculations method of the MathOperations class. It provides methods for performing addition and subtraction operations on the num1 and num2 variables. This local class allows you to encapsulate the arithmetic operations within a specific method without duplicating the code for these operations.

4.Callback Mechanism: Local classes are frequently employed in callback mechanisms, in which a callback implementation tailored to a function or section of code is provided by extending a class or implementing an interface.

example:

interface Callback {
void onSuccess(String message);
void onFailure(String message);
}

public class DataProcessor {

public void processData(String data, Callback callback) {
// Simulate processing data
boolean success = true; // Assume processing was successful

if (success) {
callback.onSuccess("Data processed successfully: " + data);
} else {
callback.onFailure("Failed to process data: " + data);
}
}

public static void main(String[] args) {
DataProcessor processor = new DataProcessor();

// Using a local class to define the callback implementation
class LocalCallback implements Callback {
@Override
public void onSuccess(String message) {
System.out.println("Success: " + message);
}

@Override
public void onFailure(String message) {
System.out.println("Failure: " + message);
}
}

// Process data and pass the local class instance as the callback
processor.processData("Some data", new LocalCallback());
}
}

DataProcessor class has a processData method that takes a Callback interface as a parameter. The main method demonstrates how to use a local class LocalCallback to provide the implementation for the Callback interface. When processData is called, the appropriate onSuccess or onFailure method of the LocalCallback instance is invoked based on the processing result.

Reach Me at:

LinkedIn , Email

--

--