when do we use keyword static in java code example
Example 1: static keyword in java
// example on static block in java.
public class StaticBlockExample
{
// static variable
static int a = 10;
static int b;
// static block
static
{
System.out.println("Static block called.");
b = a * 5;
}
public static void main(String[] args)
{
System.out.println("In main method");
System.out.println("Value of a : " + a);
System.out.println("Value of b : " + b);
}
}
Example 2: what is static keyword in java
Static keyword is used a lot in java.
Static means, you
can access those static variables
without creating an object,
just by using a class name.
This means that only one instance of
that static member is created which
is shared across all instances of the class.
Basically we use static keyword when
all members share same instance.