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:

  • Transaction Isolation Levels in JDBC The transaction isolation levels in JDBC help us to determine whether the concurrently running transactions in a DB can affect each other or not. If there are 2 or more transactions concurrently accessing the same DataBase,… Read More
  • Reading and Writing File in Java Java like other programming languages supports both reading and writing of files. For reading purpose we use FileReader and for writing we use FileWriter. FileReader Class : The FileReader class creates a Reader that … Read More
  • Creating a Basic JDBC application Today we will be creating a Basic JDBC application i.e. an application that will be able to retrieve information (student_name, roll_no, class, DOB, etc) from the database. I already gave a tutorial on connecting SQL Server… Read More
  • Java : Encryption and Decryption of Data using AES algorithm with example code There are many problems when you try encrypting a string such password, credit card nos, phone no. etc ie 1. which algorithm to use. 2. how to store the generated Key in the database. 3. should i use MD5, AES etc. Here is … Read More
  • HashMap simple usage and understanding Map is an object that stores key/volume pairs. Given a key, you can find its value. Keys must be unique, but values may be duplicated. The HashMap class uses a hash table to implementation of the map interface. This allows… Read More