wellnwill Marathahalli Bangalore

Variables in Java

Variable is a temporary place in memory. We are defining the variable with some name so that we can store the data and access the data accordingly. Syntax :

< Data type >   < variable Name > ;  // declaration
< variable Name >  =  < value >;  // initialization
int a=20 ; // Here a is variable

Types of variables: Based on data type

Based on the data type variables are classified into Primitive variable and Reference variable.

Primitive Variables :

Variables declared with primitive type are known as primitive variables. Example - int a=20;

Reference Variables :

Variables declared with user defined data type are known as reference variable. Example - String name ;

Primitive Variables Reference Variables
Variables declared with primitive type are known as primitive variables Example : int id=10 ; Variables declared with reference data types are known as reference variables Example : Employee e1 ;
Primitive variables contain data directly Reference variables contain either null or address of an object
Default value for primitive variables depends on primitive data types Default value for reference variable is null

Types of variables : Based on scope (position of the variable declaration)

Instance Variables :

  • Variables declared in a class, but outside a method, constructor or any block are known as instance variables or object level variable or non-static variable.
  • Inside each and every object, instance variable value will be different.
  • For every object a separate copy of instance variables will be created.
  • Instance variables will be created at the time of object creation.
  • Heap memory is used to store the object/data.
  • Instance variable cannot be accessed directly from static area but we can access with the help of object reference.
  • Default value will be provided by JVM for object level variable.

Static Variables :

  • If the value of a variable is same for all the objects we have to place such variable inside class area by using static keyword.
  • For each static variable only a single copy is allocated in the memory (method or class area) at the time of classloading and this memory will be shared to all the objects of the class.
  • We can access static variable either by object reference or class name. We can access static variable directly without using class name if that variable belongs with same class.
  • Static variables can be accessed directly from both static and instance areas.
  • JVM provides default value for static variable.
  • Static variables are also known as class level variables
  • We can’t declare a local variable as static.
  • Example - Static int i ;

Local Variables :

  • The variable which is declared inside a method or constructor or block is known as local variable. It is stored into stack area.
  • Local variables will be created while executing the blocks in which we declare. Once the block execution will be completed automatically local variables will be destroyed.
  • Local variable gets memory inside stack and also known as stack level variable.
  • JVM does not provide default value to local variable.
  class Test{
         int i ; --------------> Instance variable/non-static variable/object level variable
         static int j ; -------> Static variable/class level variable
         void display() {
                int c ;  ------> Local variable/stack level variable
Footer with Map