Scroll to top

Instance Variable in Java

Instance variable in Java is used by Objects to store their states. Variables that are defined without the STATIC keyword and are Outside any method declaration are Object-specific and are known as instance variables. They are called so because their values are instance specific and are not shared among instances.

If a class has an instance variable, then a new instance variable is created and initialized to a default value as part of the object creation of class or subclass.

Example of Instance Variable

class Page {
public String pageName;
// instance variable with public access
private int pageNumber;
// instance variable with private access
}

Rules for Instance variable in Java

  • Instance variables can use any of the four access levels
  • They can be marked final
  • They can be marked transient
  • They cannot be marked abstract
  • They cannot be marked synchronized
  • They cannot be marked strictfp
  • They cannot be marked, native
  • They cannot be marked static

Cheatsheet

  • Public, Private, Protected all access modifiers can be applied to Instance Variable(Default also).
  • Instance Variable can be marked final.
  • Instance Variable can be marked transient.
  • Instance Variables cannot be abstract.
  • Instance Variable cannot have a synchronized modifier.
  • Instance Variable cannot have a strictfp modifier.
  • Instance Variable cannot have the native modifier.
  • Instance Variable cannot have a Static modifier as it will become a Class level variable.
  • The instance variable will get a default value, which means the instance variable can be used without initializing it. The same is not true for Local Variable.
Instance Variable TypeDefault Value
booleanfalse
byte(byte)0
short(short) 0
int0
long0L
charu0000
float0.0f
double0.0d
Objectnull
package com.jbt;

/*
 * Here we will discuss about different type of Variables available in Java
 */
public class VariablesInJava {

	/*
	 * Below variable is INSTANCE VARIABLE as it is outside any method and it is
	 * not using STATIC modifier with it. It is using default access modifier.
	 * To know more about ACCESS MODIFIER visit appropriate section
	 */
	int instanceField;

	/*
	 * Below variable is STATIC variable as it is outside any method and it is
	 * using STATIC modifier with it. It is using default access modifier. To
	 * know more about ACCESS MODIFIER visit appropriate section
	 */
	static String staticField;

	public void method() {
		/*
		 * Below variable is LOCAL VARIABLE as it is defined inside method in
		 * class. Only modifier that can be applied on local variable is FINAL.
		 * To know more about access and non access modifier visit appropriate
		 * section.
		 *
		 * Note* : Local variable needs to initialize before they can be used.
		 * Which is not true for Static or Instance variable.
		 */
		final String localVariable = "Initial Value";
		System.out.println(localVariable);
	}

	public static void main(String args[]) {
		VariablesInJava obj = new VariablesInJava();

		/*
		 * Instance variable can only be accessed by Object of the class only as below.
		 */
		System.out.println(obj.instanceField);

		/*
		 * Static field can be accessed in two way.
		 * 1- Via Object of the class
		 * 2- Via CLASS name
		 */
		System.out.println(obj.staticField);
		System.out.println(VariablesInJava.staticField);
                System.out.println(new VariablesInJava().instanceField);
	}
}

Get hold of Static Variable in Java to understand how it is different from the Instance variable.

References

Author avatar
JBT
https://javabeginnerstutorial.com/

60 comments

  1. Hasini

    Thankz.. it is easy to understand.

  2. Macmillian

    Is it possible to assign a value to an instance variable during declaration stage?

  3. SUMIT KUMAR GUPTA

    is global variable or instance variable same or not?

  4. Waqas Shah

    Hi,
    Thank you so much for making such a great website.

    It made me understand the static topic very clearly.

  5. asdf

    Why the ***** is this comment section so toxic and full of illegible broken english

  6. alia

    Really cool blog., you have done a well job on this topic.thanks. will read on

Post a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.