Variable type in Java programming language
In this tutorial, we will discuss a concept of variable type in Java programming language
what is variable?
the variable is the name of the reserved area, which is allocated in memory for executing Java programs. A variable is assigned using the data type
Variable is a space which holds the value for execution purpose in Java programming
There are three types of variable using in Java program.
- Local variable
- instance variable
- Static Variable
Example to understand the type of varaiable
class A{
int data=40; // instant variable
static int a=25; //static variable
void method(){
int n=50; //local variable
} // end of method
}// end of class
Local variable
- The variable which are declared inside the methods,constructors or block known as local variable
- It can’t be access outside of the program.
- Need initiak value assignment before it use
Example
public class yourage{
public void Age(){
int age=0; //local variable
age=age+8;
System.out.println(“Age:”+age);
}
public static void main(String args[]){
yourage priya=new yourage();
priya.Age();
}
}
in tha above code ‘age’ is local variable This local vaiable is use only method Age.
Instance variable
The variable that is declared inside the class but out side the method, constructors or block are called instance variable. it is not declared as a static
class A{
int data=50; //instance variable;
void method(){
int n=90; //local variable
} //end of method
} // end of class
Static variable
The variable that is declared as static is called static variable. it can not be local.
class A{
int data=50; //instance variable;
static int m=1000; // static variable;
void method(){
int n=90; //local variable
} //end of method
} // end of class
Example
class static1
{
static int id=1000;
static boolean condition=true;
static char c=’A’;
public static void main (String args[]){
System.out.println(Test.condition);
System.out.println(Test.id);
System.out.println(Test.c);
}
}
The above code is executed, it produces the following result
Suggested for you
Data type in C language
Data type in C++ language