Updated April 1, 2023
Introduction to Local Variable in Java
In Java, we have a Local variable that can only be defined inside a method; the scope for the local variable is within the block inside which it gets defined. Outside the block, we cannot even access the variable because we do not know in real whether it exists or not for us. Local variables are useful when we need some temporary variable to hold the values for us inside the block, and we do not require those variables for other methods or for the entire class in java, then in such cases, we can define the variable as local in java to full fill the requirement. In the coming section of the tutorial, we will see its internal working, implementation, and their usages of how we can use this while programming in java.
Syntax
As we know that 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 see below;
Variable_type varibale_name = value_to_hold;
As you can see in the above syntax, we can define the local variables like this; let’s take a closer look at the practice syntax, which will give us better clarity for this to understand and start using this in a program for beginners; see below;
e.g. :
String demo = ""my variable !!;
In the coming section, we will see its working in more detail and some important rule to define the local variable in java to avoid the compile-time errors.
How Local variable works in Java?
As we already know what local variables are in java, but in this section, we will see some basic rules to define the local variable inside the class method in java or inside any block. This variable is very useful when we have to use the variable for a very limited scope in our code; then, we should go with the local variables. They act as temporary variables for us to use hold the vale and vanish when the particular piece of code stops or finishes its execution. In this section first, we will see the rule to define and use the local variables in java then we will see where does java store their local variables let’s get started to see below;
1. Methods with local variable declaration
Now, we will have a look at the method creation, and we will define one local variable inside that method. For reference, see the below code;
e.g. :
public String getmsg(){
int temp = 0;
Syste.out.println(temp);
}
Above is the sample piece of code that we can use to define the local variable; here, we are creating one method to declare and initiate one local variable for us. This scope for this variable is only to the method, not outside the method or other methods present in the class.
2. Static local variable
In java, we cannot have a static local variable declaring inside a method that is not static here. Because static variables are directly associated with the class level. Let’s take a piece of code that define this scenario and will give us compile-time error while creating it; see below;
e.g. :
public void test(){
static String teststr = "hello"; // compile time error
}
3. Static local variable inside static method
In java, we cannot even define the static variable inside the static method in java. The same problem will happen as above; it will give us a compile-time error. For reference, see the below code;
e.g. :
public static void test(){
static String teststr = "hello"; // compile time error
}
4. Final local variable
In java, we can define the final local variable inside the method. We just need to have the final keyword associated with it. For reference, see the below code;
e.g. :
public void test(){
final String teststr = "hello";
}
Points to remember while using the local variable in java :
- Java stores the local variable inside the stack memory space, not in heap memory.
- Local variables are specific to the block only, not outside the method block.
- Even class other methods cannot even access the local variable; they are not even aware of them.
- We cannot create a static local variable because they are specific to the class level.
Examples of Local Variable in Java
Given below are the examples of Local Variable in Java:
Example #1
In this example, we are creating a method that is returning the local variable as the result of the calling function.
Code:
package com.practise.tets;
public class SimpleArray {
public static void main(String[] args) {
SimpleArray simpleArray = new SimpleArray();
String result = simpleArray.test();
System.out.println("result is ::" + result);
}
public String test(){
String localstr = "hello world !! ";
return localstr;
}
}
Output:
Example #2
In this example, we assign value to the local variable by passing the value from the calling function as the parameter, a sample example for beginners to understand better.
Code:
package com.practise.tets;
public class SimpleArray {
public static void main(String[] args) {
SimpleArray simpleArray = new SimpleArray();
String result = simpleArray.test("passing string to local variable !!!");
System.out.println("result is ::" + result);
}
public String test(String str){
String localstr = str;
return localstr;
}
}
Output:
Example #3
In this example, we will have a compile-time error while trying to create the static local variable.
Code:
package com.practise.tets;
import java.util.ArrayList;
import java.util.List;
public class SimpleArray {
public static void main(String[] args) {
SimpleArray simpleArray = new SimpleArray();
String result = simpleArray.test();
System.out.println("result is ::" + result);
}
public String test(){
static String localstr = "hello word !!!";
return localstr;
}
}
Output:
Conclusion
As we have already seen in the tutorial, local variables are important and how we can use them in our program to treat them like temporary variables to hold any value for performing some task on them. They are easy to sue and handle for the developers as well.
Recommended Articles
This is a guide to Local Variable in Java. Here we discuss its internal working, implementation, and usages of how we can use this while programming in java. You may also have a look at the following articles to learn more –