Static keyword example
Example of static Variable
Example of static method
Example of static block
Example of static Variable
public class StaticVariableExample
{
static int Instances;
StaticVariableExample()
{
Instances++;
}
public static void main(String[] args)
{
StaticVariableExample sve1 = new StaticVariableExample();
System.out.println("No. of instances for sve1 : "
+ sve1.Instances);
System.out.println("");
StaticVariableExample sve2 = new StaticVariableExample();
System.out.println("No. of instances for sve1 : "
+ sve1.Instances);
System.out.println("No. of instances for sve2 : "
+ sve2.Instances);
System.out.println("");
StaticVariableExample sve3 = new StaticVariableExample();
System.out.println("No. of instances for sve1 : "
+ sve1.Instances);
System.out.println("No. of instances for sve2 : "
+ sve2.Instances);
System.out.println("No. of instances for sve3 : "
+ sve3.Instances);
System.out.println("");
StaticVariableExample sve4 = new StaticVariableExample();
System.out.println("No. of instances for sve1 : "
+ sve1.Instances);
System.out.println("No. of instances for sve2 : "
+ sve2.Instances);
System.out.println("No. of instances for sve3 : "
+ sve3.Instances);
System.out.println("No. of instances for sve4 : "
+ sve4.Instances);
System.out.println("");
StaticVariableExample sve5 = new StaticVariableExample();
System.out.println("No. of instances for sve1 : "
+ sve1.Instances);
System.out.println("No. of instances for sve2 : "
+ sve2.Instances);
System.out.println("No. of instances for sve3 : "
+ sve3.Instances);
System.out.println("No. of instances for sve4 : "
+ sve4.Instances);
System.out.println("No. of instances for sve5 : "
+ sve5.Instances);
}
}
Output
No. of instances for sve1 : 1 No. of instances for sve1 : 2 No. of instances for sve2 : 2 No. of instances for sve1 : 3 No. of instances for sve2 : 3 No. of instances for sve3 : 3 No. of instances for sve1 : 4 No. of instances for sve2 : 4 No. of instances for sve3 : 4 No. of instances for sve4 : 4 No. of instances for sve1 : 5 No. of instances for sve2 : 5 No. of instances for sve3 : 5 No. of instances for sve4 : 5 No. of instances for sve5 : 5
Example of static method
Example of static method
public class StaticMethodExample
{
static int i=0;
static void getmethod()
{
i++;
System.out.println("Value of i is" +i);
}
}
class TestStatic
{
public static void main(String args[])
{
StaticMethodExample.getmethod();
}
}
Output
Value of i is 1
Example of static block
Example of static block
public class StaticBlockExample
{
static int[] values = new int[10];
static
{
System.out.println("Initialization block Running");
for (int i = 0; i < values.length; i++)
{
values[i] = (int) (10.0 * Math.random());
}
}
void listValues()
{
for (int value : values)
{
System.out.println(value);
}
}
public static void main(String[] args)
{
StaticBlockExample example = new StaticBlockExample();
System.out.println("\nValue of First object:");
example.listValues();
example = new StaticBlockExample();
System.out.println("\nValue of Second object:");
example.listValues();
}
}
Output
Initialization block Running Value of First object: 9 7 5 6 9 0 2 2 8 3 Value of Second object: 9 7 5 6 9 0 2 2 8 3



