What is the variable in Java

Variable in Java is the name of the memory location. Variable is used to store the information or value that is required by a java program.

What do I mean by the name of the memory location?

Let’s understand this in detail using a java statement int i= 10; Here “i” is the name of the variable, “10” is the value and “int” is the type of the value called Data type.

So when I write this statement(int i=10) or line of code in a java program the value(10) will store somewhere in the memory and this value can be dynamic. So will provide a name to that memory location that is called variable in Java.

To Identify the type of value stored in the variable java use the data types(int, float, double), etc. In the above statement data type of the variable is “int”. Understand the Data type in Java in Detail.

Types of the variable in Java

Mainly, There are 3 types of variable in Java that is defined only on the basis of the scope of the variable.

  •  Local Variable
  •  Instance Variable
  •  Static Variable

Local Variable in Java

A variable that is defined under the method or constructor is called a local variable in Java. The scope of the local variable is only in the method only. Other methods of the same method also cannot access it.

Local variables defined locally and accessible locally only, for example, I have a method called void sum(){} and I define a variable in this method that will be a local variable for this method. Other methods can’t access it.

public void sum() {

int i = 10; //Local variable
//i will not be access out of this method

}

Local variable in Java is must initialize.

Instance Variable in Java

A non-static variable defines in the class is called class variable or Instance variable in java. Instance variables always declares in the class and out of the method constructor or a block.

Points to remember for Instance variable in Java

Instance variable is accessed by only the object of a class.

The default value of the instance variable in 0. So it’s not mandatory to initialize the instance variable.

  •  Instance variable is also called a class variable.
  •  Instance variable is not declare as static.
  •  Instance variable can be used to access modifiers.
  •  Instance variable can be final.
  •  Instance variable can not be strictfp, abstract, synchronized.

Example:

package com.demo;

import java.util.*;

class Main {

int i = 20; //Instance variable
public static void main(String[] args) {
Main obj = new Main();
System.out.println(obj.i);
}

}

Static Variable in Java

Static variable in java is declared with the static keyword. It is used to define the common property of the object.

Static variables are useful for memory management because they shared all the instances of a class.

Example Suppose we are going to write a java program to generate Marksheet of the students of a class. So the common property for the students will be College_Name, Semester_Name. In the last example, we will see this problem in detail.

Points to remember for Static variable in Java

  • Static variable is useful for memory management.
  • We can call a static variable without the object.
  • Static variable initialize when class is load.
  • Its not mandatory to initialize static variables in java.
  • Default value of a static variable in Java is 0.
package com.demo;

import java.util.*;

class Main {

static int i = 20; //static variable
public static void main(String[] args) {

System.out.println(i);
}

}

Now, Let’s understand all the types of variables in Java with a program.

Problem: Write a java program to Generate Marksheet of the students for semester 1

package com.demo;

import java.util.*;

class Main {

int roll_no;//instance variable 
String student_name;//Instance variable 
static String college_name ="SGS"; //this is the static variable 

//constructor.........
Main(int r, String n){ 
roll_no = r; 
student_name = n; 
} 

void printResult (){
String msg = "Marksheet"; // This is the local variable
System.out.println(msg);
System.out.println(roll_no+" "+student_name+" "+college_name);
} 

public static void main(String[] args) {
Main obj1 = new Main(1010,"Bhupi");
obj1.printResult();

}
}

Output