Variables Types in java application:

There are 3 kinds of variables in Java program. They are: 1. Local Variable. 2. Instance variable. 3. Class variable / Static variable.
Note: There is no global variable concept in Java, but you can simulate global variable functionality by using static variables of java.

Local Variable:

Any variable that you declare in the function/method is called as local variable.

Instance Variable:

Any variable that you declare at class level and which is not static, other than locals

Class Variable:

Any variable that you declare at class level and which is static, other than locals and instance variables.

Stack Segment:

All local variables will be stored in Stack.


a. All the instance variables will be stored in heap. b. All the objects created dynamically using 'new' operator will be stored in heap. (Arrays will also be created in heap even if you don't use new keyword)

Data Segment:

All the static and class variables will be stored in data segment.

Code Segment:

It will have the final machine code generated by JVM.

Example program, depicting the variables of Java

Program 1:

Program to find area of a rectangle.

Here we will depict with this example by showing where our variables will be stored, once you run this program.
File name :
class Rectangle
	int len, bred; //instance variables

	public void init(int l , int b)//l,b are local variables as they are in function
		len = l;
		bred = b;
	public void findArea()
		//int area is also local variable
		int area = len * bred;								                                
public class MainClass
	public static void main(String[] args)
		Rectangle r = new Rectangle(); //Here r is local variable.
		r. init(3,4);
Program 2: This is an example of Pen class. Every pen object has data and some functions. Data of pen object are - ink level, capstate. Ink level says how much ink is there in a pen object, and capstate says whether cap is opened or closed. Methods of pen object - init() function which will initialize the pen object with ink, and write() method .
Below diagram is the representation of pen object: pen object 2
File name :
class PenClass
	int inkLevel;
	boolean capstate;
	public void init(int n)
		inkLevel = n;
		capstate = false;
	public void write()
              public class MainClass
	public static void main(String[ ] args)
		PenClass p = new PenClass();
		PenClass q = new PenClass();
		q.init (100);
                                 q.write ();

Memory architecture for above java program:

memory architecture2
In the above program, in main() PenClass p = new PenClass(); -> In this line many things will happen.
a. Since p is a local variable, it will be allocated in stack.
b. And in the same line right hand side new Pen(); will create an object (in heap). Since we are using new keyword it will be stored in heap.
c. Once it creates object in heap, every object will have some address. (Just like every house will have some address). That address will be stored in variable "p".
d. The object created in heap will have inkLevel, and capState. Because we are creating object for Pen class, which has 2 instance varilables (inkLevel & capState). As per rule all instance variables will be stored in heap.
Program 3: Find area of the circle, and draw memory architecture.
Note: To create constancts in java we use "final" keyword. Here pi value is constant. area of circle
Below is the java program for this example.
File name : Mainclass
In the above program, there is one design flaw: That is , assume that we have created 100 circle objects. Then how many places "pi" will be stored? Ans: 100 places.
But we know that pi value will never change as it is a constant. Then what is the point of storing in 100 times? Whey can't we store in a single place, and ask all the 100 circle objects to share it? This can be done by making it as a "static" variable.

Property of static variable:

It will be created only once, no matter how many objects you create for that class.


Make all the final variables to static final variables to avoid the memory wastage because static variables can be created only once at the program loading time. No matter how many objects you create system will maintain only one copy of static variable.( This is called as memory optimization).
Example: I have a Pen class with 2 functions initialize, and write. When ever some body creates an object, I want to find out how many number of objects(pens) have been created so far.
File name :