32
static Keyword
Hello People I hope you all are doing well, In this article I'll be writing about the static keyword in Java.
The word static in the context of general language means something that is unchangeable or stable or constant. Whereas in context of programming static is a keyword, if word static is used as prefix in method name or variable name, then that will become static in nature. Something static in nature, in context of programming, is a property of class i.e., it will be initialized when the class load will happen.
In Java static keyword is used in java mainly for memory management. It is used with variables, methods, blocks and nested class. The main method of a class is generally labeled static.
public static void main(String [] args) {
//line of code
//line of code....
}
A variable is called a static variable when it is declared with the static keyword. In Java, a static variable is a class variable i.e., static variable shares the common property among all the objects of the same class. It saves the memory, hence, makes program memory efficient. It doesn’t have any unique value, for example, School’s name, Company’s name. Compiler does not allow static local variable.
public class Students {
static String schoolName = "ABC Public School";
public static void main(String [] args) {
System.out.println("Name of school is " +schoolName);
}
}
You can run your code online here
Static variables can be accessed outside the class by calling with the class name i.e., ClassName.variableName.
i.e.,
Students.schoolName;
A method becomes static when it is initialized with a static keyword. It is a method which belongs to the class and not to the object. A static method can call only other static methods. We call them without creating an object of the class.
public class Students {
static void calculatePercentage() {
// this is a static method
}
void calculatePercentage() {
// this is a non-static method
}
}
Now for non-static method you will need to create object of class then invoke method
Students st = new Students();
st.calculatePercentage();
Whereas for static method we will invoke method without creating a new instance of the class
Students.calculatePercentage();
Why is the main method static?
Because program execution begins from it, and no object exists before calling it. This save lots of memory by JVM.
The static blocks are used to initialize the static data members of the class and it gets executed before the main method at the time of classloading. This is a special block that Java supports. This code inside static block is executed only once.
import java.util.Date;
public class Students {
static {
Date date = new Date();
System.out.println("Today's date is :" + date);
}
public static void main(String [] args) {
System.out.println("Hello");
}
}
You can run your code online here
There can be more than one static block, and they can appear anywhere in the class body. Each of them are run in the sequence of their announcement.
public class Students {
static {
System.out.println("Hello this is first static block");
}
static {
System.out.println("Hello this is second static block");
}
static {
System.out.println("Hello this is third static block");
}
public static void main(String [] args) {
System.out.println("Hello this is inside main");
}
}
You can run your code online here
We can create a class within a class in Java. This is called nested class.
public class Students {
class Marks {
}
}
A nested class is marked with static modifier is called the static nested class. The static nested classes do not have access to any instance members of the enclosing outer class, the static nested classes only have access to static members of the outer class, including private ones.
public class Students {
static String schoolName = "ABC Public School";
static class Inner {
void name() {
System.out.println("School name is "+ schoolName);
}
}
public static void main(String [] args) {
Students.Inner st = new Students.Inner();
st.name();
}
}
You can run your code online here
Cover:- Rajat Gour
My personal blog blog.ritvikdubey.com
32