11/14/11

Static Keyword in Java

While you are programming you would want to use some class members independently of any object of that class. Normally a class member is accessed with the help of the object of that class. However, it is possible to create class members that can be used by itself. To create such a member, the keyword static has to precede its declaration. 

When a class member is declared as static, it can be accessed before any object of that class is created and without reference to any object. Both methods and variable can be declared as static. The best example to understand is our main() which is declared static. Its is static because it must be called before any object exist.

Instance variables declared as static are, generally global variables. 

Methods declared as static have several restrictions :

  1. They can only call other static  methods
  2. They must only access static data.
  3. They cannot use this or super in anyway (Super  is a keyword used in Inheritance ).
Below is a code that will help you understand the use of static. In the example discussed all methods and variables are declared as static. So as an when you execute this code the 1st static declaration gets executed, then 2nd  and so on.

// Demonstrate static variables, methods, and blocks.
class ExampleStatic {
     static int a = 5;
     static int b;
     
     static void setValMeth(int x) {
           System.out.println("x = " + x);
           System.out.println("a = " + a);
           System.out.println("b = " + b);
     }

     static {
           System.out.println("Static block initialized.");
           b = a * 5;
     }

     public static void main(String args[]) {
           setValMeth(50);
     }
}




As soon as the ExampleStatic class is loaded, all of the static statements are run. First, a is set to 5, then the static block executes (printing a message), and finally, b is initialized to a * 5 or 25. Then main( ) is called, which calls meth( ), passing 50 to x. The three println( ) statements refer to the two static variables a and b, as well as to the local variable x.


Note It is illegal to refer to any instance variables inside of a static method.


Here is the output of the program:

Static block initialized.
x = 50
a = 5
b = 25

Static methods and variables can be used independently of the object. To do so you only need to specify the classname followed by the dot operator and method Ex :

classname.method();


The above method is same as the calling of non-static methods through object reference variable. The static variable can also be accessed in the same way.

Here is an example in which there is static method callMe() and the static  variables a,b are explained outside of their class.

class Check1 {
    static int a = 42;
    static int b = 99;
    static void callme() {
        System.out.println("a = " + a);
        }
    }

class Check2 {
    public static void main(String args[]) {
        Check1.callme();
        System.out.println("b = " + Check1.b);
        }
    }

OUTPUT :


a = 42
b = 99

SHARE THIS POST:

Related Posts:

  • OOPs Concept: Encapsulation Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction. RFAZHE7YNFV8 Encapsulation is the technique of making the fields in a class private and provid… Read More
  • OOPs Concept : Polymorphism Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Any java object that can pass more t… Read More
  • Java Programming with Eclipse : Basics This is intended to be a basic guide to using Eclipse. This guide is by no means comprehensive, but is sufficient to give you enough knowledge to work on your projects. Running and using Ecli… Read More
  • Object Oriented Programming (OOP) Explanation Object-oriented programming (OOP) is a programming paradigm that uses "objects" to design applications and computer programs. It utilizes several techniques from previously established paradigms, including inheritance… Read More
  • OOPs Concept : Inheritance After discussing Encapsulation, now its time for 'Inheritance' as OOP Concept. Inheritance can be defined as the process where one object acquires the properties of another. With the use of inheritance the information is … Read More