Table of Contents
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. 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.In such cases, we can define the variable as local in java to fulfill the requirement. In this article, 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. See below for better understanding;
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 !!;
Learn to code from industry experts! Enroll here
How Local variable works in Java?
As we already know what local variables are in java, 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;
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.
Learn to code from industry experts! Enroll here
Class Variables
1: What is the default value of a boolean in Java?
Class variables are declared using the static keyword. All instances of the class share the same value of the class variable. The value of a class variable can be accessed and modified by using class methods or instance methods. Once the value is modified, all instances of the class are updated to share the same value.
Declare Class Variables
Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.
The following is the syntax to declare class variables.
<static> <data_type> <variable_name>;
where,
<static> implies that all instances of the class share the same static variable
<data_type> is the data type of the variable.
<variable_name> is the name of the variable.
The following code snippet shows the declaration of class variables.
class Book{ static int price=0; }
Access Class Variables
Class variables are referenced by the class name itself. This makes it clear that they are class variables. The following is the syntax to access class variables.
<class_name>.<variable_name>
where,
<class_name> is the name of the class where the variable is declared.
<variable_name> is the variable name.
The following code snippet demonstrates how to declare and reference class variables.
class LocalDemo{ static int price=1; public static int getCost() { return LocalDemo.price; } }
Grab the opportunity to learn Java with Entri! Click Here
Difference Between Class Variables And Instance Variables In Java
Class Variables | Instance Variables |
Class variables are declared with keyword static. | Instance variables are declared without static keyword. |
Class variables are common to all instances of a class. These variables are shared between the objects of a class. | Instance variables are not shared between the objects of a class. Each instance will have their own copy of instance variables. |
As class variables are common to all objects of a class, changes made to these variables through one object will reflect in another. | As each object will have its own copy of instance variables, changes made to these variables through one object will not reflect in another object. |
Class variables can be accessed using either class name or object reference. | Instance variables can be accessed only through object reference. |