Local Variable in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

A local variable has a value just within a certain method or function. The variable is unknown to the program outside of the scope of the function. Static variables cannot be declared for local variables.

Introduction to Local Variables in Java

In the programming language Java, a programmer can define a local variable only within a function; the scope of the local variable is within the section or the block in which the local variable is declared. The variable is not accessible outside the block since we don't know whether it exists in reality.

Local variables are essential whenever we need a temporary variable to store values inside the block but do not need those variables for other functions or the whole class in the programming language java. In such circumstances, we can define the variable as local in java to meet the requirement.

How Local Variable Works in Java ?

In the programming language Java, a local variable is defined within a class function or any block. Local variables are essential when we just need the variable for a very limited range in our program, in that case, we should be using local variables. They serve as temporary variables that hold the value and disappear when the specific section of code stops or completes its execution.

The scope of local variables is restricted to function. A variable of this kind is only available within the function or block in which it is declared. The scope of the local variables begins with the line on which they are defined and continues until the method's closing curly brace.

Let's understand the concept with the help of an example :

The sample code given above shows that we can make use of local variables as seen above, we are constructing one function to declare as well as initialize a local variable for us. This variable's scope is limited to the function and does not extend to other methods in the class.

Usability of Using Final Local Variables

  • Significantly, to declare a local variable using the final keyword in any inner class, the local variables are declared as final inside the inner class It is concerned with the special accessor methods created the inner class is created. The local variables which are not declared as final cannot be used by inner classes.
  • It may enable the Just In Time compiler which is the compiler used in Java to optimize code because the value of the variable will not alter. This can shorten the program's processing time.

Important Points of Local Final Variable

Initialization of the variable is not Mandatory :

Although the local variable is initialized with the modifier final, the programmer must execute initialization if only he intends to utilize it. Otherwise, he is not required to perform the process of initialization even though the local variable is declared as final.

Output :


Considering local variables in Java programming, the only appropriate modifier is the final keyword :

The term final in Java is said to be the only modifier that can be applied to any local variable in Java. If we use another modification mistakenly, while trying to run the program the programmer will face a compile-time error.

Output :

On trying to run the java program given above we will get an error that says the illegal start of expression and the execution of the program won't be successful.

Declaration of Local Variables

A block ({ … }) surrounds each local variable declaration statement. Local variables can also be declared in the header of a "for" expression. It is executed in this situation as if it were part of a local variable declaration statement.

For instance, Consider the given statement below :

In the preceding example, we have mentioned int j=0, which is a declaration of a local variable. Its scope is limited to the for loop only.

Syntax of Local Variables

As we know in java, local variables are specific to the block, not outside that; let’s take a look at the syntax for declaring the local variable for better understanding sees below :

We can define local variables as shown above in the syntax, let's look more closely at the syntax to declare local variables, which would provide us more clarity for this to comprehend and begin using in a beginner's program.

Program Example of Local Variables with Explanation

Output :

Explanation :
the variable named age1 declared in the program given above is a local variable. This particular variable is defined within the PrgNew() method, and its reach is confined to this particular method alone. The variable age1 cannot be accessed by any other method.

Conclusion

  • Local variables exist exclusively within the function block and as discussed they cannot be used outside of it.
  • Even the other functions of the class are unable to access the local variables, they are unaware of their existence.
  • Java is known to place the local variable in the stack memory area rather than storing it in the heap memory space.
  • We cannot construct a static local variable since they are class-specific.