Skip to main content
Working of static variables, methods




                       http://improvejava.blogspot.in
                                                        1
Objective


On completion of this period, you would be able to know :


          • Static member

          • Static variables

          • Static methods




                               http://improvejava.blogspot.in
Recap

In the previous lesson, we have discussed
   • ‘this’ pointer
   • Usage of ‘this’ pointer
   • Related programs




               http://improvejava.blogspot.in
Static Member


• Normally a class member must be accessed only in
  conjunction with an object of its class
• However, it is possible to create a member that can be used
  by itself, without reference to a specific instance
• Such a member is called static member




                     http://improvejava.blogspot.in
                                                                4
Static Member

• To create such a member, precede its declaration with the
  keyword static

• When a member is declared static, it can be accessed before
  any objects of its class are created, and without reference to any
  object

• The most common example of a static member is main()

• main() is declared as static because it must be called before any
  object exist.
                        http://improvejava.blogspot.in
                                                                       5
Static Variable
• You can declare both methods and variables to be static

• Instance variables declared as static are, essentially,
  global variables

• Such variables are called as class variables

• For a class variable only one copy of memory is allotted
  regardless of any number of objects created




                     http://improvejava.blogspot.in
                                                             6
Example Program : Non-Static Variable
class A {
      int x;                                       non static variable
      public static void main (String args[]) {
          A obj_a = new A();
          obj_a.x = 10;
          System.out.println (“ value of x in obj_a ”+ obj_a.x);
          A obj_b = new A();
          System.out.println(“value of x in obj_b ”+ obj_b.x);
      }
}
Output : x value in object pointed by obj_a 10
         x value in object pointed by obj_b “garbage value”


                            http://improvejava.blogspot.in
                                                                         7
Example Program :Non-Static Variable
Memory allocation for the objects in the above program


     • A obj_a = new A();                       • A obj_b = new A();
     • obj_a.x = 10;



            x :10                                        x :gb


            obj_a   for non-static variables             obj_b
                    memory is allocated for
                     each object individually

                        http://improvejava.blogspot.in
                                                                       8
Example program :Static Variable

class A {
      static int x;
      public static void main (String args[]) {
         A obj_a = new A();
         obj_a.x = 10;
         System.out.println(“value of x in obj_a ”+ obj_a.x);
         A obj_b = new A();
         System.out.println(“value of x in obj_b ”+ obj_b.x);
      }
}



                     http://improvejava.blogspot.in
Example Program :Non-Static Variable
Memory allocation for the objects in the above program


     • A obj_a = new A();                      • A obj_b = new A();
     • obj_a.x = 10;


                               x :10


                    For static variables memory
            obj_a   is allocated for each object
                                                        obj_b
                    in common place
                    not inside object
                       http://improvejava.blogspot.in
                                                                      10
Static Methods


• Methods declared as static have several restrictions

   • They can only call other static methods

   • They must only access static data

   • They cannot refer to this or super in any way




                          http://improvejava.blogspot.in
                                                           11
Example Program : Static Methods
class A {
      static int x=10;
      static void call (int y) {
              System.out.println(“ x value is : ”+ x);
              System.out.println(“ y value is : ”+ y);
      }
      public static void main (String args[]) {
         call(20);
      }
}
Output : x value is : 10
           y value is : 2                      call() is called without
                                                        creating object because
                                                        call() is static method


                       http://improvejava.blogspot.in
Calling Static Methods

• Normally to call a method, its object is need
• To call total_mks() of Student class we need to create student
  object and then call total_mks on thaat objecxt
                • Student s = new Student();
                • s.total_mks();
• To call static method, there is no need to create an object of the
  class
• To call it use the class name directly
            • ClassName.methodName();



                          http://improvejava.blogspot.in
                                                                       13
Example program : Calling Static Methods
class A {
  static int a = 10;
  static int b = 20;
  static void callme() {
          System.out.println(“ a: ”+a);
  }
}
class B {
     public static void main() {                          calling static method
          A.callme();                                     without using object
          System.out.println(“ b: ”+A.b);                 reference
     }
}
  Output:
                                                  static variable is
     a: 10
                                                  Accessed without
     b: 20
                                                  object reference
                       http://improvejava.blogspot.in
                                                                                  14
Summary


• Static member
• Static variables
• Static methods




                     http://improvejava.blogspot.in
                                                      15
Quiz
1. Static variable memory is created
   a. Like instance variable
   b. Separate memory for each object
   c. Common memory for all object
   d. None




                   http://improvejava.blogspot.in
                                                    16
Quiz                       contd..
2. Static variables or methods are accessed:
   a. using object
   b. using reference
   c. using class name from outside class.
   d. none




                   http://improvejava.blogspot.in
Frequently Asked Questions


1. Explain the use of static variables?

2. Explain the concept of static methods?




                    http://improvejava.blogspot.in
                                                     18
Assignment

1. Write an Employee class assuming appropriate data
   and method members
   Create objects for the above class, and display
   how many number of objects are created by using
   static member concept




                   http://improvejava.blogspot.in