Static keyword

Objects are instances of class. All objects of same class have same attributes. Every object maintain a separate copy of its data member. Static keyword is used to make a variable that is shared among objects of same class. Static variable is associated with class. One static variable will be created for one class, no matter how many objects are created. Objects only maintain separate copy of class data member not for static variable. Static variable initialised automatically by default value of corresponding data type. Static variable retains its value during method call.


Static variable in Class


	  // file:stat.java 
// www.jgyan.com
class stat
{
int a;
static int b;
stat()
{
a=10;
}
void show()
{
System.out.println("data member:"+a);
System.out.println("Static member:"+b);
}
void change(int m, int n)
{
a=m;
b=n;
}
}
class tw
{
public static void main(String arg[])
{
stat s=new stat();
stat k=new stat();

s.show();
k.show();
s.change(20,30);
s.show();
k.show();
System.out.println("static:"+stat.b); // Static variable can be accessed using class name.
}
}
/*Outpu:
data member:10 --+
Static member:0 |----> Object s data 10 and 0
data member:10 ----+
Static member:0 |-----> Object k data 10 and 0
data member:20 ----+
Static member:30 |----> Object s data after changing value 20 and 30
data member:10 ----+
Static member:30 |----> Object k data static variable changed to 30.
static:30
*/

Static method

Static methods are declared with static keyword. Static methods can access only static data. Static methods are associated with class not with objects. Although static method can be called by using object but they can't access object's data.


Example :


	   // file: stat.java
// www.jgyan.com
class stat
{
static int a;
static void increase()
{
a++;
System.out.println("static variable:"+a);
}
}
class tw
{
public static void main(String arg[])
{
stat.increase();

stat s=new stat();
s.increase();
}
}
/*Output:
static variable:1
static variable:2
*/

Static Block

If we want to execute a piece of code, which should execute only once at the time of class loading in memory. At that time static block is useful. Static block has static keyword then body of block enclosed by curly braces.

Syntax :

    static
{
statement1;
statement2;
.........
.........
.........
.........
statementn
}

Code written inside static block execute only one time when first object will be created of that class and class will be loaded in memory first time.


Example :


	  // file: stat.java
// www.jgyan.com
class stat
{
static
{
System.out.println("Static Block executed");
}
}
class tw
{
public static void main(String arg[])
{
stat s=new stat();
stat k=new stat();
stat l=new stat();
}
}
/*Output:
Static Block executed
*/

In above program class stat has a static block.

Important ! Static block gets executed on loading of class first time. When first object if created, class is loaded in memory and static block executed. Static block executed only one time.

On creation of second and third object static block will not be executed.