Instance Variable in Java

This entry is part 7 of 34 in the series Core Java Course

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

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

 

Series Navigation<< Local Variable in JavaJava Reference Variable >>

36 Comments Instance Variable in Java

    1. Vivekanand Gautam

      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

      Reply
  1. Vishwanath T S

    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);

    Reply
  2. shweta

    Hi Bharat,

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

    Reply
    1. kiran

      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’

      Reply
  3. Tabish Khan

    @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. :)

    Reply
  4. nupur

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

    Reply
  5. Sanjay Varma

    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.

    Reply
  6. vinod

    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.. :)

    Reply
  7. Santos

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

    Reply

Leave A Comment

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