Variables In Java With Diagram

Don’t get confuse with static/instance variable and static/instance blocks. So let’s get clear one by one. In this section we will see what is static / instance variable and block and also its exact place to use.

Variable is the name given to the storage area (container/memory location) to store/hold value. Stored data value can vary/change during program execution hence name is Variable. Quality of the data value it can hold will be decided by data types.

Quickly have a look on primitive data types of java and its capacity to store value. We have there are 8 primitive data types in java and they are listed below,

SL. No. Data Type Capacity
1 byte 1 byte
2 short 2 byte
3 int 4 byte
4 long 8 byte
5 float 4 byte
6 double 8 byte
7 char 2 byte
8 boolean 1 bit

* 1 byte = 8 bits

Let’s come back to variable. Variable should be declared before its use. That means before using variable we need to specify the capacity of the variable that it can hold.

Syntax: Data-type variable-name;


Variable in java syntax

Here age is the variable name and it can hold up to 4 bytes of data value.
Let’s look at types of variables,

Variables in java, Types of variables
Types of Variables in java

Types of variables

Local variable

Variables which are declared inside method, block or constructor is called Local Variable. Scope/access of these variables is only its respective block, method or constructor. Local variable cannot be declared with keyword ‘static’. Local variables should be assigned before its use because there is no default value for local variable.


public class SampleDemoClass {

	public static void main(String[] args) {
		String name = "Raj"; 	   // ‘name’ is local variable
		int age;		   // ‘age’ is local variable		
		System.out.println(name);  //no error 
		System.out.println(age);   //compile error >> Initialize variable                                              		

Static variable

Variable which are prefixed with static keyword is called static variable. Static variables are also called as class variables as it belongs to class. Static variables are declared inside the class but outside method, constructor or block. For every class we have only one copy of static variable in memory irrespective of how many object present for that class. To access static variable no need to create object but using class name can be accessed. i.e.
Static variable have default value.


public class SampleDemo {
	static String name = "Rajkumar";	// 'name' is static variable
	static int age;				// 'age' is static variable	
	static boolean mStatus;			// 'mStatus' is static variable
	static String fatherName;		// 'fatherName' is static variable
	static float weight;			// 'weight' is static variable	

	public static void main(String[] args) {
		System.out.println(; 		//no error 
		System.out.println(SampleDemo.age);		//no error
		System.out.println(SampleDemo.mStatus);		//no error
		System.out.println(SampleDemo.fatherName);	//no error
		System.out.println(SampleDemo.weight);		//no error

Instance variable

Variable which are declared without static keyword are called instance variable. Instance variables are declared inside class outside method, constructor or block. Each object will have its own copy of instance variable. That means when ever object is created copy of instance variable assigned to it. In order to access instance variable need to create object of respective class. i.e. object.instance_variable_name;
Instance variable have default value.


public class SampleDemo {
	String name = "Rajkumar";		// 'name' is instance variable
	int age;				// 'age' is instance variable	
	boolean mStatus;			// 'mStatus' is instance variable
	String fatherName;			// 'fatherName' is instance variable
	float weight;				// 'weight' is instance variable	

	public static void main(String[] args) {
		SampleDemo cObj = new SampleDemo();	//object ‘cObj’ created for class SampleDemo	
		System.out.println(; 		//no error 
		System.out.println(cObj.age);		//no error
		System.out.println(cObj.mStatus);	//no error
		System.out.println(cObj.fatherName);	//no error
		System.out.println(cObj.weight);	//no error

Recommended article for you,

Introduction To Java Programming | Java History | Java Versions

Hey, Now it is your time! Drop a comment if more details needed or if any update requires. Your comments are more valuable to improve our site to help others.

6 thoughts on “Variables In Java With Diagram”

  1. obviously like your web site but you need to take a look at the spelling on several of your posts. A number of them are rife with spelling issues and I to find it very troublesome to tell the truth nevertheless I’ll certainly come again again.

  2. Heya i am for the primary time here. I came across this board and
    I in finding It truly useful & it helped me out a lot.
    I’m hoping to present one thing back and help others such as you aided me.

  3. Thanks for a marvelous posting! I certainly enjoyed
    reading it, you happen to be a great author.I will be
    sure to bookmark your blog and may come back at some point.

    I want to encourage you continue your great writing,
    have a nice morning!

Comments are closed.