Variables in JAVA | Readstall

Variables in JAVA

07-02-19 Devendra Ganta 0 comment

variables:

A variable provides us with named storage and that our program
can manipulate each variable i java has specific type, which determines
the size and layout of the variable memory.

there are 3 types of variables in java
1.Local variables
2.Global variables
3.Static variables
.

Local variables:

These are declared with in the method, constructors.
Local variables are created when the method, constructor is enter and the
variable will be destroyed once it is exit the method.

Local variables are visible only within the declared method.

Global variables:

Instance variables are declared in a class.But outside a method.
Instance variables hold values that must be referenced by calling the
variable name inside the class

Static variables (Class variables):

Static variables also known as class variables are declared with
‘static’ keyword in a class. Static variables are created when the program
starts and destroyed when the program stops.

EXAMPLE:

class VariableExample
{
int data=50;//instance variable
static int m=100;//static variable
void method()
{
int n=90;//local variable
}
System.out.println(“instance variable %d”,data);
System.out.println(“static variable %d”,m);
System.out.println(“local variable %d”,n);
}
OUTPUT:

instance variable=50
static variable=100
local variable=90



Leave a reply