Instance Variable in Java

Here we will learn about instance variables in Java with the help of an example. Instance variables are used by Objects to store their states. Variables which 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.

Example of Instance Variable

class Page {
public String pageName;
// instance variable with public access
private int pageNumber;
// instance variable with private access
}
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);
	}
}

Rules for Instance variable

  • 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 3 access modifiers can be applied to Instance Variable(Default also).
  • Instance variable will get default value means instance variable can be used without initializing it. Same is not true for Local Variable.
  • Instance Variable can be marked final.
  • Instance Variable can be marked transient.
  • Instance Variable can not be abstract.
  • Instance Variable can not have synchroonized modifier.
  • Instance Variable can not have strictfp modifier.
  • Instance Variable can not have native modifier.
  • Instance Variable can not have Static modifier as it will becomes Class level variable.
Instance Variable TypeDefault Value
booleanfalse
byte(byte)0
short(short) 0
int0
long0L
charu0000
float0.0f
double0.0d
Objectnull
Series Navigation<< Local Variable in JavaJava Reference Variable >>
By | 2017-08-14T10:55:53+00:00 April 20th, 2012|Core Java|42 Comments

About the Author:

42 Comments

  1. shivaraj.j September 25, 2013 at 4:54 am - Reply

    nice it is……..i want learn from this

    • karim February 25, 2017 at 5:50 am - Reply

      very good

  2. shivaraj.j September 25, 2013 at 4:56 am - Reply

    nice it is……..i want learn from this ……….tihs is fist time i saw this……….

    • Xyz April 11, 2014 at 6:03 am - Reply

      Galeej bwack!!!!!!!!!!!!!!!!!!!!!!!!!!!

  3. RK February 26, 2014 at 10:07 am - Reply

    Can be abstract – Could you explain what it means for an instance variable to be abstract

  4. nidhi paul March 12, 2014 at 3:50 pm - Reply

    Awesme xplanation
    thanx
    concept gt cleared
    keep up the good wrk *–*

  5. Bharatgpatel March 15, 2014 at 9:17 am - Reply

    I want learned java

    • Vivekanand Gautam March 15, 2014 at 12:16 pm - Reply

      Hi Bharat,

      You said you want to learn Java. As you can see there are articles i have written on site which would be helpful for you.

      Or if you want to direct learn Java by us do let me know. We are going to start online classes. Where you can start by enrolling, It will be paid classes.

      Thanks

      • sunil September 10, 2016 at 6:24 pm - Reply

        Sir I want learn java
        Can you pleasexplain send me your contact profile to
        For online class

        • J Singh September 14, 2016 at 8:56 pm - Reply

          Hi Sunil,

          Right now i am not having enough time for online classes.

          Regards

  6. Ramesh M May 1, 2014 at 5:50 pm - Reply

    You can’t declare an instance variable as abstract.The program wont compile.Please check

  7. Vishwanath T S July 7, 2014 at 10:05 am - Reply

    Static variables can also be accessed directly:
    /*
    * Static field can be accessed in two way.
    * 1- Via Object of the class
    * 2- Via CLASS name
    *3-Directly calling the static variable itself(within static methods)
    */
    System.out.println(obj.staticField);
    System.out.println(VariablesInJava.staticField);
    System.out.println(staticField);

  8. shweta July 15, 2014 at 2:37 pm - Reply

    Hi Bharat,

    instance variable can be static but you have mention it cannot be static ..as per my knowledge its wrong statement.

    • kiran November 2, 2014 at 3:04 pm - Reply

      bharat: instance variables can’t be static ..if you define static variable just outside the method it can’t be instance variable….if you define variables with static keyword ..it becomes class variable(which is not instance variable..)..
      class variables are not part of Object.where as instance variables are part of Object..Object stores its state in Non Static fields i.e instance variables also called ‘fields’

    • karim February 25, 2017 at 5:52 am - Reply

      no instance variable cannot be static because we have learned in the rules of the instance variable

  9. Tabish Khan November 1, 2014 at 3:10 am - Reply

    @Shweta, sister, a variable declared as “static”, makes it a class variable, not an instance variable. There’s a small and a simple difference between the two. 🙂

  10. ashish February 13, 2015 at 6:04 pm - Reply

    what is the main use of constructor in java core.

  11. nupur May 1, 2015 at 4:53 am - Reply

    You said instance variables can be abstract, but its not true, why we need to mark any variable abstract?!

    • V Gautam May 1, 2015 at 7:44 am - Reply

      Typo problem. Same has been resolved.

  12. Sanjay Varma July 22, 2015 at 2:06 pm - Reply

    Instance variable can be access in this way too

    System.out.println(new VariablesInJava().instanceField)

    This is recommended when your calling Instance variable not more than once.

  13. Muaz Razaq July 25, 2015 at 2:00 am - Reply

    Great Work, Nice Explaination, I Simply Understood The Difference

  14. Achyuth November 12, 2015 at 11:36 am - Reply

    why don’t use the these keywords like Abstract,strictfp,native.

  15. Rajendra December 3, 2015 at 7:09 am - Reply

    good

  16. shah imran January 21, 2016 at 9:01 am - Reply

    I want to learn java programming

    • J Singh January 27, 2016 at 10:52 am - Reply

      Please like our page. Some times we organize online classes you can attend those classes if required.

      Regards

  17. florito January 31, 2016 at 2:00 am - Reply

    its good to read this

  18. arunkumar March 4, 2016 at 11:36 am - Reply

    confusing with lots of keywords, how to remember their usage..

    • J Singh March 5, 2016 at 2:57 am - Reply

      No Matter how much are there you have to remember. 🙂

  19. Tino nigeria March 13, 2016 at 2:06 pm - Reply

    I want to follow you along, pls show concern.

  20. srinidhi May 15, 2016 at 5:54 am - Reply

    good work .than you

  21. srinidhi May 15, 2016 at 5:55 am - Reply

    Good work .thank you

  22. lazzy June 20, 2016 at 11:36 am - Reply

    what is java????

    • surya September 17, 2016 at 10:31 am - Reply

      Java is a high level programming language.It was developed by Sun Microsystem in 1995.

  23. Muskan Rathi August 23, 2016 at 2:44 pm - Reply

    Wat is d difference between class variable nd instance variable

  24. vinod September 9, 2016 at 8:34 pm - Reply

    it is really helpful for beginners to learn java basics..
    is it possible for you to give some more examples for related topics.. thank you.. 🙂

    • J Singh September 14, 2016 at 8:56 pm - Reply

      We do try to add as much information as possible. And will keep doing the same.

  25. Santos November 14, 2016 at 10:33 am - Reply

    more clarification about java programing in 1.Local variable 2.Instance variable 3.Static variable
    and give the example by using demostration

  26. Ragaranjàni July 27, 2017 at 2:05 pm - Reply

    Very useful to me…

  27. suchitra August 18, 2017 at 11:08 am - Reply

    Previously i was having difficulty with all these topic , but now i m confident with these topic.

    A lot of thanx to you….

  28. north africa August 25, 2017 at 11:24 am - Reply

    good website for java, i will visit again

  29. camisetas futbol 2016 August 29, 2017 at 9:24 pm - Reply

    This design is spectacular! You certainly know how to keep a reader entertained.

    Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Excellent job.
    I really enjoyed what you had to say, and more than that, how you presented it.

    Too cool!

Leave A Comment