Search This Blog

Monday 16 July 2012

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 statichas to precede its declaration.

When a class member is declared asstatic, 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 :


They can only call other static methods
They must only access static data.
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 variablesa,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

No comments:

Post a Comment

Thanks for feedback,
Your opinion will surely help me to enhance the quality of the content.