Variables in java and types of variable ,Local variable ,Instance Variable ,Static Variable With Example

What is Variable ?

Variables are used to store the constant values by using these values we are achieving project
requirements.and a variable always assigned with the data type.
Generally project variables are :

Employee project : int eid; String ename; double esal;
Student project : int sid; String sname; double smarks;
 Variables are also known as fields of a class or properties of a class.
 All variables must have a data type. That data type it may be

Primitive type ( int, float,…)  Eg .Int a=10;
 Array type     Eg. Int[ ] a;
class type        Eg. Test t;
 Enum type     Eg .Week k;
Interface type.          Eg.It1 I;
Variable argument type.      Eg.Int… a;
Annotation type       Eg.  Override e;

 Syntax of Variable - The basic syntax of a variable is given below.
      int a=10
//Where "a "is variable

Variable declaration -
Variable declaration is composed of three components in order,
Zero or more modifiers.
The variable type
The variable name.
Constant value/ literal

public int a=10;

public ----> modifier (specify permission)
int ---->      data type (represent type of the variable)
a ---->         variable name
10 ---->       constant value or literal;

; ---->          statement terminator

Types of variable in java
There are three types of variables in java
1. Local variables.
2. Instance variables.
3. Static variables.

1-Local Variable -The variables which are declare inside a method or constructor or blocks those variables are called Local Variable 


Example of Local Variable 

class Testing
{
public static void main(String[] args)
{
int a=10; //local variables
int b=20; //local variables
System.out.println(a+b);
}
}





*Point to remember about Local Variable
 1)It is possible to access local variables only inside the method or constructor or blocks only, it is not possible to access outside of method or constructor or blocks.

Class Test 
{
void add()
int a=10;
System.out.println(a); //possible
}
void mul()
System.out.println(a);//not-possible
} }

2) Local variables memory allocated when method starts & memory released when method completed.
3) The local variables are stored in stack memory.
4)There are five types of memory areas in java:
  • Heap memory
  • Stack memory
  • Method area
  • SCP(String constant pool) memory
  •  Native method stacks
Instance variables (non-static variables):

The Instance Variables are define as the variables which are declare inside a class but outside of methods are called instance variables. Instance Variables are also called as the non- static variable .
  • The scope (permission) of instance variable is inside the class having global visibility.
  • Instance variables memory allocated during object creation& memory released when object is destroyed.
  •  Instance variables are stored in heap memory. 


Example of Instance Variable

class Testing
{
int a=10; //instance variables
int b=20; //instance variables
public static void main(String[] args)
{
Testing t=new Testing();
System.out.println(t.a);
System.out.println(t.b);
}

}

Static variables OR class variables:
The Static variable is define as the variables which are declared inside the class but outside of the methods with static modifier are called static variables.Static variable also known as the class variables
  • Scope of the static variables with in the class global visibility.
  • Static variables memory allocated during .class file loading and memory released at .class file unloading time.
  • Static variables are stored in method area.
Example of Static Varaible

class Testing
{
static int a=5000;  //static variables
static int b=4000;  //static variables
public static void main(String[] args) //static method
{
 System.out.println(Testing.a);
System.out.println(Testing.b);
}
}

Static variables calling: - There are three ways to access the static variable

1) Direct accessing- We can access static variable directly
2) By using reference variable.-we can access static variable by reference variable 
3) By using class-name. (Project level use this approach)-we can access static variable by using class name 

Example of static variable calling

class Testing
{
static int a=100; //static variable
public static void main(String[] args)
{
System.out.println(a);         //1-way(directly possible)
System.out.println(Testing.a); //2-way(By using class name)

Testing t=new Testing();

System.out.println(t.a);      //3-way(By using reference variable)
}
}

Variables VS default values:

Case 1:-for the instance & static variables JVM will assign default values.

class Testing
{
int a;
Static boolean b;
public static void main(String[] args)
{
Testing t=new Testing();
System.out.println(t.a);
System.out.println(Testing.b);
}
}
Case 2:-
Local variables JVM does not provide default values.
In java before using local variables must initialize some values to the variables otherwise compiler
generates compilation error “variable a might not have been initialized”

class Test

{
 public static void main(String[] args)
{
 int a;
System.out.println(a);//error: variable a might not have been initialized
}
};

Instance Variable vs Static Variable

Post a Comment

1 Comments