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.
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
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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.