In Java, static keyword is mainly used for memory management. It can be used with variables, blocks, methods and nested classes. It is a keyword that is used to share the same variable or method of a given class. When a member is declared static, then it essentially means that the member is shared by all the instances of a class without making copies of per instance.
In Simple words, It is a keyword used to define class member.
Thus static is a non-class modifier in Java and can be applied to the following members:
In Java, static blocks are used to initialize the static variables/data member.
In java static block is executed at the time when the first object of the class is created (precisely at the time of classloading) or when the static member inside the block is used.
class Simple {
// static variables
static int a = 20;
static int b;
static int min;
// static blocks
static {
System.out.println("First Static block.");
b = a * 2;
}
static {
System.out.println("Second Static block.");
min = 25;
}
// static method
static void display() {
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("min = " + min);
}
public static void main(String args[]) {
// calling the static method
display();
}
}
Output:
First Static block.
Second Static block.
a = 20
b = 40
min = 25
In java a member variable of a class that is declared as static is called the Static Variable. It is also called as the Class variable.
When a variable is declared as static, then a single copy of static variable is created and shared by all the objects.
class CollegeStudent
{
int rollno; //instance variable
String name;
static String college ="Xyz";//static variable
//constructor
CollegeStudent(int r, String n)
{
rollno = r;
name = n;
}
//method to display the values
void display ()
{
System.out.println(rollno+" "+name+" "+college);
}
}
class Simple{
public static void main(String args[])
{
CollegeStudent stu1 = new CollegeStudent(101,"Peter");
CollegeStudent stu2 = new CollegeStudent(202,"Tony Stark");
stu1.display();
stu2.display();
}
}
Output:
101 Peter Xyz
202 Tony Stark Xyz
In java when a method is declared with static keyword, it is known as static method. static methods also belong to a class instead of the object, and so they can be called without creating the object of the class in which they reside.
Some points related to static method:
class Simple
{
// static method
static void sMethod()
{
System.out.println("Hyy i'm static method in Java...");
}
public static void main(String[] args)
{
sMethod();
}
}
Output:
Hyy i'm static method in Java...
class Cal {
// static method
static int multiply(int a, int b){
return a * b;
}
}
class Simple {
public static void main( String[] args ) {
// create an instance of the Cal class
Cal st = new Cal();
// call the static method
System.out.println("2 * 5 = " + Cal.multiply(2,5));
}
}
Output:
2 * 5 = 10
In Java, we can declare a class inside another class. Such classes are known as nested classes.
Nested classes are 2 types:
Nested static class doesn’t need a reference of Outer class. In this case, a static class cannot access non-static members of the Outer class.