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

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

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,…

1 year 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…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

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

1 year 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…

1 year ago

This website uses cookies.