TYPES OF ATTRIBUTES

Types OF Attributes

  1. Instance attributes or non-static attributes.
  2. Static attributes

Instance attributes or non-static attributes:

  • Attributes without static keyword are called as instance attributes.
  • Instance attributes are specific or private to each Object .
  • Instance attributes should be accessed by using object or object ref var.

Static attributes:

  • Attributes with static keyword is called as static attributes.
  • Static attributes are common to all Objects and not a part of object.
  • Static attributes are accessed by class name only, even if we try to access by using object or obj ref var after compilation it will convert into corresponding ref var data type .
class Person{
                                int age;
                                double height;
                                double weight;
                                static String food;
                                static int legs;
                                public static void main(String[] args) {
                                Person p1=new Person();
                                Person p2=new Person();
                                p1.age=20;
                                p1.height=50.0;
                                p1.weight=60.0;
                                p1.food="grace";
                                p1.legs=2;
                                p2.age=30;
                                p2.height=40.0;
                                p2.weight=70.0;
                                p2.food="rice";
                                p2.legs=4;
                                System.out.println(p1.age);     /* 20   */
                                System.out.println(p2.age);     /* 30   */
                                System.out.println(p1.height);  /* 50.0 */
                                System.out.println(p2.height);  /* 40.0 */
                                System.out.println(p1.food);    /* rice */
                                System.out.println(p1.legs);    /* 4    */
                                System.out.println(p2.food);    /* rice */
                                System.out.println(p2.legs);    /* 4    */
                     }

                            
                        
If we run the above program we will get two objects in memory like below
Java Setup Wizard
Static and Non-Static Variable Storage in Memory .

Explanations:

  • In the above program age,height, weight are declared as instance attributes these are specific to each object.
  • Food, legs are defined as the static attributes these are common to all objects.
  • Static attributes are stored in separate location common to all objects.
  • If any object change the static attribute values will reflect to all remaining objects. So last updated value will available in static attribute.
  • But in case of non-static attributes if we change any attribute value it will reflect to that object only and doesn't effect to any other objects.