Static Keyword


This image has an empty alt attribute; its file name is image-9.png

The static keyword is used mainly for memory management. We can apply static keyword with variables, methods, blocks and nested classes (defining a class within another class). 

Examples;

//static variables

Static int count;

//static method

Static void x(){}

//static block

Static{

//code

}

//static nested class

Class main{

Static class nestedStatic{

}

}

The static keyword belongs to the class (class variables) rather than an instance of the class (instance variables).  Don`t worry about this now. We will be looking into different variable types in the coming post.

The static keyword allows the sharing of the same variable or methods in the same class. It needs to be declared with the keyword static that it could be known as a static variable, static method, static block or static class. 

Example;

Leave a comment