It is same to global variable of other languages. Static keyword in java is used for memory management.
Static keyword is used as three ways.
- Static data keyword
- Static block
- Static method
In Java there are three types of memory:
- Stack Area : All the local Variable of a methods or constructor can be store in Stack Area.
- Heap Area : All the Objects are created in Heap Area.
- Class Area : All the Static data member are created in Class Area.
Static Data Member : It Represents class Attributes. Static keyword is used to declaring static data member. static data member executed only one that is created only once and all the object of a class can share that single copy of static data member, that is static data member is common for all objects of that class.
static int a=5;
public static void main(String args)
first x=new first();
first y=new first();
Static Block : It is also called static initializer block. It is mainly use to initialize static data member. static block executed only once but automatically.
Static Method : When a simple method is associated with static keyword known as static method. static method can be executed without creating class object that is it can be executed with it class name only.
private static void example()