Variables in Java

variables in Java

Variables in Java programming language

  • A variable is a basic unit to store a value into that variable. Variables in Java  defined by declaring and initializing.
  • A variable is defined by the combination of type and identifier and value (if  necessary)
  • type is the type of that variable to be declared and identifier is the name of the variable.
  • The name of the variable should not contain any spaces , that is the name of the variable must be one single word.
  • Variables which are declared inside the method parenthesis are called arguments.

For Example : void myMethod(int x) {  code }

Syntax :    type identifier = value ;


  int x = 10; // declaring and initializing

int y ; // this is just declaring

float y = 10.2;

boolean b = true;

String myname = “sam”;

String myName = “java”;

  • Java is case-sensitive , the name of the variable ‘myname’ is different from ‘myName’. When you are trying to access any variable in a program make sure of case sensitivity.
  • Variables are either stored in stack or heap. Java has two types of memory :stack and heap


Different categories of variables

Variables are of two categories :

  1. primitive type : When you are trying to  declare a variable using the primitive data types. For example int , char , float etc.
  2. Reference type : These reference type variables are useful in the time of object creation. Reference variables are nothing but classes , interfaces , abstract classes. Creating our own type of variable is considered to be as reference type.

Example : String myname ; here String is one of the class in Java API.

myClass object ;

  • In the above example , object is the reference variable to the class ‘myClass‘.
  • The memory allocation of the reference variable is different which can be explained in object creation chapter.


Different types of variables and memory allocation

In your Java program you can mention two types of variables : instance variables and local variables.

variables in java

  1. Instance variables
  • Variables which are declared inside the class and outside the methods are called instance variables.
  • In C-Lang , it is called as global variables.
  • Instance variables are available to all the methods in the class , which means the scope of the variable(availability) is through out the class. Any method or block can access instance variables.
  • Whenever you create an object to the class , all the instance variables will automatically stored inside the object you created.

2. Local variables 

  1. Variables which are declared inside a method or block are referred as local variables.


  • A block is begun with an opening curly brace ‘{‘ and ended by a closing  curly brace ‘}’ . A block defines a scope , thus each time you start a new block  you are creating a new scope.
  • Anything could be a block like methods , control statements (‘if’ , ‘while’ , ‘for’ etc)

 2. The scope of the local variables are available within that block only , outside blocks or methods cannot access other’s local variables.

variables in java memory

3. Memory allocation of the local variables are in stack memory. Whenever JVM enters your block or method ,it creates a memory in stack and stores all the local variables. When the JVM completed executing all the statements inside a block and then the JVM will destroy the memory inside the stack which it created while entering.


4.  Therefore , local variables will not hold any space in the memory until and unless the block or method is called.



6 thoughts on “Variables in Java

  1. Woah! I’m really digging the template/theme of this
    blog. It’s simple, yet effective. A lot of times it’s hard to get that “perfect balance” between user friendliness and visual appeal.
    I must say you’ve done a superb job with this. In addition, the blog loads extremely quick for me on Safari.
    Outstanding Blog!

  2. Appreciating the time and energy you put into your blog and in depth information you offer. It’s awesome to come across a blog every once in a while that isn’t the same out of date rehashed information. Fantastic read! I’ve bookmarked your site and I’m adding your RSS feeds to my Google account.

Leave a Reply

Your email address will not be published. Required fields are marked *