• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Method Local Inner Class in Java

July 25, 2017 By j2eereference Leave a Comment

Method local inner class in Java is limited to a code section, and it can be processed only inside the same code section it is declared in. Hence it can never execute modifiers. A local inner class instance can be delivered as argument and retrieved from methods, and is available inside a valid scope.

 Purpose of Method Local Inner class

The only limitation in method local inner class is that a local parameter can be executed only when it is defined ‘final’. The basis for this limitation associates primarily to multi-threading problems and makes sure that every variable has definite values when accessed from local inner class. The method executing the local parameters can be called after the execution of the method, within which the local inner class was declared. As a result, the local parameters will no longer retain their values. Hence the values must be fixed before creating the local inner class object. If required, a non-final variable can be copied into a final variable which is subsequently executed by the local inner class.

The following example program will not compile unless the statement System.out.println(b); is eliminated.

 

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class J2EEReferenceOuterClass {
private int a = 5;
 
public void exampleMethod() {
int b = 10; // here the variable is not declared final
final int c = 20;
 
class J2EEReferenceLocalInnerClass {
public void accessSampleOuter() {
System.out.println(a);
 
System.out.println(b); // (Error: Cannot refer to non-final
// variable b inside an inner class
// defined in a different method)
 
System.out.println(c);
}
}
J2EEReferenceLocalInnerClass innercls = new J2EEReferenceLocalInnerClass();
innercls.accessSampleOuter();
}
}

Java
1
2
3
4
5
6
public class LocalInnerTest {
public static void main(String[] args) {
J2EEReferenceOuterClass outercls = new J2EEReferenceOuterClass();
outercls.exampleMethod();
}
}

OUTPUT

Compilation Error

On declaring the variable ‘b’ as final, the output for the program would be

5

10

20

On eliminating the statement ‘System.out.println(b);’ the output would be,

5

20

 

It is obvious that a method local inner class can never be addressed (i) public, (ii) private, (iii) protected, (iv) static or (v) transient as it is local to a method. Either abstract or final can be applied as modifiers to a method local inner class.

Related Posts

  • Implementing queue in java
  • TreeSet vs ConcurrentSkipListSet
  • How to create immutable class?
  • Implementing Stack in java
  • What is ReentrantLock?
  • What is Semaphore in java
  • Why AtomicInteger class
  • What is CyclicBarrier?
  • CountDownLatch in java
  • Prototype design pattern

Filed Under: Core Java

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • What is parallel Stream
  • reduce method of the Stream class
  • Difference between the findFirst() and findAny() method
  • intern() method of String class
  • SOLID – Five principles of object-oriented software design
  • Java Coding Best Practices
  • How to use lambda expression effectively
  • Enhanced pseudo-Random Number Generators in java17
  • How to use Foreign-Memory Access API
  • Pattern Matching for instanceof
  • Text Blocks – Feature added in Java17
  • Record – The new feature added in java 17
  • What is Sealed Class
  • Features added in Java 17
  • Java Buzzwords

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.