- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
final local variable in Java
Local Variable
Local variables are declared in methods, constructors, or blocks.
Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or block.
Access modifiers cannot be used for local variables.
Local variables are visible only within the declared method, constructor, or block.
Local variables are implemented at stack level internally.
There is no default value for local variables, so local variables should be declared and an initial value should be assigned before the first use.
final Local Variable
final is the only allowed access modifier for local variables.
final local variable is not required to be initialized during declaration.
final local variable allows compiler to generate an optimized code.
final local variable can be used by anonymous inner class or in anonymous methods.
Example
abstract class AnonymousInner { public abstract void display(); } public class Tester { public static void main(String args[]) { final int value; value = 100; AnonymousInner inner = new AnonymousInner() { public void display() { System.out.println("Accessing value: " + value); } }; inner.display(); } }
Output
Accessing value: 100
- Related Articles
- Final variable in Java
- Instance variable as final in Java
- Can a method local inner class access the local final variables in Java?
- What is a final variable in java?
- Unreachable statement using final variable in Java
- Final local variables in C#
- What is static blank final variable in Java?
- Can we initialize blank final variable in Java
- How to declare a local variable in Java?
- What is blank final variable? What are Static blank final variables in Java?
- How to use a final or effectively final variable in lambda expression in Java?
- What is a blank uninitialized final variable in java?
- What is blank or uninitialized final variable in Java?
- Unreachable statement using the non-final variable in Java
- Local Variable Type Inference or LVTI in Java 10
