instance variable in java

In this post we will discuss –

  1. What is instance variable and couple of points related to it.
  2. Access modifiers used with instance variable.
  3. Memory allocation for instance variable.
  4. How to use instance variable in real time project.

 

What is instance variable ?

  1. Instance variable must defined inside class and outside of method or block.
  2. Instance variable can’t be marked as static.
  3. Instance variable can be final.
  4. Instance variable can’t be synchronized, strictfp or abstract.
  5. We can use all access modifier with instance variable(we will see later).

Let’s see a simple program which will cover all above points –

public class InstanceTest1 {
	private int a = 10;
	int b = 20;
	protected int c = 30;
	public int d = 40;

	// abstract int e = 50; //we can't make instance variable as abstract it
	// will say remove invalid access modifiers
	// static int f = 60; // it will be static variable rather than instance
	// variable.
	// synchronized int g = 50;//we can't make instance variable as synchronized
	// it will say remove invalid access modifiers
	// same for strictfp and native

	final int h = 70;

	public static void main(String[] args) {
		System.out.println("this is example of instance variable");
	}
}

 

Explain access modifiers used with instance variable ?

As we discussed we can used all four access modifiers with instance variable. Let’s see what will be impact for these access modifiers.

Private –  

If we define instance variable as private we can only access that variable inside class.

class Test4 {
	private int b = 10;
}

public class InstanceVariableExp1 {

	private int a = 10;

	public static void main(String[] args) {
		InstanceVariableExp1 instanceVariableExp1 = new InstanceVariableExp1();

		// definitly we need to create object we can't access instance variable
		// directly inside static context.
		System.out.println(" value of a is " + instanceVariableExp1.a);

		// if we try to access variable b which is inside Test4 we can't
		Test4 test4 = new Test4();
		// System.out.println(test4.b); // it will give compilation error

	}

}

Default – 

In case of default access modifier we can access that variable outside of class but in same package.

public class InstanceVariableExp1 {

	int a = 10;

	public static void main(String[] args) {
		InstanceVariableExp1 instanceVariableExp1 = new InstanceVariableExp1();

		// definitly we need to create object we can't access instance variable
		// directly inside static context.
		System.out.println(" value of a is " + instanceVariableExp1.a);

	}

}

protected – 

We can access instance variable out side of class in same package also we can access instance variable out side of package through inheritance. Here this is very important point for default and protected access modifier which is helpful for interview purpose.

package instancevariablejavatute;

public class InstanceVariableExp1 {

	protected int a = 10;

	public static void main(String[] args) {
		InstanceVariableExp1 instanceVariableExp1 = new InstanceVariableExp1();

		// definitly we need to create object we can't access instance variable
		// directly inside static context.
		System.out.println(" value of a is " + instanceVariableExp1.a);

	}

}

Note – here package is difference.

package staticblockexample;

import instancevariablejavatute.InstanceVariableExp1;

public class DefaultTest extends InstanceVariableExp1 {
	public static void main(String[] args) {
		DefaultTest exp1 = new DefaultTest();
		System.out.println("value of a is " + exp1.a);
	}
}

 

public – 

We can access that instance variable in whole application in any package.

 

Explain memory allocation for instance variable?

will update later.

 

 

 

How to use instance variable in real time project?

  1. In real time application we make instance variable  private most of time and use as encapsulation.
  2. We rarely define instance variable protected and public.
  3. if we are using hibernate then we always make instance variable as private in entity and provide getter-setter.