java

Variable type in Java programming language

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 java language

Data type in C language

Data type in Python language

Data type in C++ language

 

Karmehavannan

Recent Posts

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

11 months ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

11 months ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

11 months ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

11 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.