Why we use static in Java
Many reserved keywords in Java cannot be used as variable names or identifiers. Java uses static keywords frequently because of its effective memory management system. In most cases, you must first construct an instance or object of a class to access variables or methods within it. There may be instances, though, where you simply need to access a few of a class's methods or variables and don't want to make a new class instance specifically for that purpose. In this situation, Java's static keyword can be used.
The static keyword can be used in Java with methods, blocks, variables, and nested classes. Alternatively, if you use the static keyword with a variable or a method inside a class, these static members will remain the same for each instance of that class you create, and you won't be able to alter or edit them. To access these members for those classes, you don't even have to create an instance of an object, and simply using the class name will give you access to them. A Java class's main method frequently includes its static keyword.
If a class has any members marked as static, even before the class starts, all of the static members can be accessed and made active. In contrast, when an object disappears or leaves the scope, non-static members of the same class will also vanish.
Programs using static keywords:
StaticMethod.java
class StaticMethod
{
// static variable declaration
static int static_count = 5;
// instance variable declaration
// we can't access the instance variable in the static method
// you can understand it in this program
int inst_var = 10;
// static method declaration
static void static demo()
{
static_count = 20;
System.out.println("static method StaticDemo "+static_count);
// inst_var = 20; // compilation error "error: non-static variable inst_var cannot be referenced from a static context"
//instMethod (); // compilation error "non-static method instMethod () cannot be referenced from a static context"
//System.out.println (super.static_count); // compiler error "non-static variable super cannot be referenced from a static context"
}
// instance method declaration
void instMethod ()
{
System.out.println ("instance method instance Method");
}
public static void main (String [] args)
{
staticDemo ();
}
}
You can see that there are two methods in the above program. While instMethod is an instance method, StaticDemo is a static method. Additionally, there are two variables: inst_var is an instance variable, and static_count is a static variable.
Instance variables, calling non-static methods, and referring to super in a static context all result in compilation errors when the program with the above lines is executed. These represent the static method's drawbacks.
The program above only runs smoothly and generates the output shown below when we comment on the three lines.
Output:

Important Points about Static:
- The static variables are initialized using the static block. When a class is loaded into memory, this block is executed. Multiple Static blocks may exist in a class, and they will all run in the same order.
- When we declare not-static members as global in a class, we are unable to access the static block. Instead of declaring non-static variables, we must create static variables if we want to access a static block.
- Static variables are shared by all instances (or objects) of the class because they are class-level variables. Class Variables are another name for static variables.
- Static methods provide you with direct access to static variables.
- Static variables are shared by each class instance.
- Static variables are set before a class is loaded before any objects are generated from that class, and before any static methods are called from that class.
- Constants are final static variables. Final variables must always be initialized; failure to do so will result in a compilation error.
- However, the only way to access non-static variables and methods is through objects. Class variables can be accessed by static methods without using a class instance (static variables).
- Static methods can be directly accessed by both static and non-static methods.
- The main static method doesn't use an object to access static variables.
- Both static and non-static methods can directly access a static method because they cannot be overridden.