Variable Types in Java

In the Data Type section you have seen the different type of variables that can be created in Java. Now from a different angle variable can be divided in this three groups.

Instance Variable:

The variables which are accessible through objects.

Consider the example below:

package com.example;

public class InstanceVariableExample {

	int a;

	double b;

	String abc;
	public void printValues (){
		System.out.println("The variables values are: " + a + " , " + b + " and " + abc);

	public static void main(String[] args) {

InstanceVariableExample instanceVariableExample = new InstanceVariableExample();
		instanceVariableExample.a = 10;
		instanceVariableExample.b = 12.5; = "Java Program.";




The variables values are: 10 , 12.5 and Java Program.

The class have three variables. When java creates an object, in the memory space the variables along with the values will be stored. The instance variable are created when Java creates object.

All the objects of the class will have the same set of instance variables, and can store different values into those variable.

If you do not initialize variables, then they gets the default value, like for int 0. Instance variable have to be declared outside any methods or constructor.

Look, as the instance variables are declared at the class level, outside any method or constructor, all the instance variables are visible to any method or the constructor.

Class/Static variable

The instance variable values can be changed by any objects.

But you may want to declare some variables whose values remains same for all the objects.

No matter how many objects are created, there is only one copy of these kind of variables. Java provides a special keyword for these kind of variable, which is ‘static’.

Check the below code:

package com.example;

public class StaticVariablesExample {

	static int a = 10;

	static double b = 10.5;

	public static void main(String[] args) {
StaticVariablesExample staticVariablesExample1 = new StaticVariablesExample();
		staticVariablesExample1.a = 14;
StaticVariablesExample staticVariablesExample2 = new StaticVariablesExample();

		staticVariablesExample2.a = 50;


Output :



Here in this class we have two ‘static’ variables; a and b.

Interesting thing happening in the main() method. We have created an object staticVariablesExample1 and assigning 14 to the variable ‘a’.

Again we are creating a second object staticVariablesExample2 and assigning 50 to the variable ‘a’. Now we are printing the value of staticVariablesExample1.a.

Run the program, you will get an output as: 50!!

Why? That is the principle of ‘static’ variable. There is only one copy of static variable, it does not matter how many object you create for the class.

When you create a new object, java checks whether there are any static variables in the class.

Each static variable has a single copy stored in the memory all the objects share the same copy no separate space is allocated in the memory for each object. This is opposite to instance variable, where for each variable each object gets a copy.

So, in the above program, there is only a single copy of variable ‘a’. First you change the value to 14, then you are changing it to 50.

Last statement stays valid, because there is only one copy. That’s why you are getting an output 50.

Now just a tip. As you have understood that static variable are not attached with any object, they are the property of class itself, do not try to access any static variables with object.

Like we have done above: staticVariablesExample1.a = 14; this code is not wrong, but this statement does not make any sense either.

Instead, whenever you have to access ‘static’ variable inside the same class, access it directly. Like: a = 14; It will have the same effect as staticVariablesExample1.a = 14;

And when you want to access ‘static’ variables from a different class, use it with the class name.

For example check the below code:


package com.example;

public class AccessStaticVariables {

	int abc = StaticVariablesExample.a;

Here in this class I want to use the static variable ‘a’ of StaticVariablesExample class. Since ‘a’ is a static variable, I can access that variable with the class name, like we have done above.

Local variable:

Local variables are those variable which you declare inside a particular program block, i.e. inside method, constructor or if block.

Check the below code :

package com.example;

public class LocalVariableExample {

	int a = 10;

	double b = 14.9;

	public void methodA() {
		int a = 5;
		int x = 50;
System.out.println("The value of a inside method is : " + a + " and the value of x is : " + x);

	public static void main(String[] args) {
		if (true) {
			double b = 10.9;
			System.out.println("The value of b inside if block is : " + b);

		LocalVariableExample localVariableExample = new LocalVariableExample();


Output :

The value of b inside if block is : 10.9
The value of a inside method is : 5 and the value of x is : 50

Here we have two instance variable ‘a’ and ‘b’. Again inside methodA() we have a variable with the same name ‘a’ and another variable ‘x’.

The variables which are declared inside the methodA() are local variable of the method. Only the method has access to these variables.

No other part of code have no knowledge of this variables. You will get a compilation error if you try to use the variable ‘x’ outside methodA()!

Interesting part is inside methodA().

When we are writing this line: int a = 5;

 java compiler does not consider the variable which we have defined as instance variable (int a = 10;). This feature is called ‘hiding’.

The compiler hides the instance variable and use the local variable.

Same thing happening inside the ‘if’ block with variable ‘b’.

Run the code and the output will be:

The value of b inside if block is : 10.9
The value of a inside method is : 5 and the value of x is : 50

Now, for experimental purpose, ‘comment out’ the first line inside methodA().

public void methodA() {
		//int a = 5;
		int x = 50;
		System.out.println("The value of a inside method is : " + a + " and the 
value of x is : " + x);

Can you imagine the output now? The value of ‘a’ will remain 10, and so the output will be:

The value of b inside if block is : 10.9

The value of a inside method is : 10 and the value of x is : 50

Next > < Prev
Scroll to Top