static is a non-access modifier in Java which is applicable for the following:
- nested classes
To create a static member(block,variable,method,nested class), precede its declaration with the keyword static. When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. For example, in below java program, we are accessing static method m1() without creating any object of Test class.
If you need to do computation in order to initialize your static variables, you can declare a static block that gets executed exactly once, when the class is first loaded. Consider the following java program demonstrating use of static blocks.
Static block initialized. from main Value of a : 10 Value of b : 40
For Detailed article on static blocks, see static blocks
When a variable is declared as static, then a single copy of variable is created and shared among all objects at class level. Static variables are, essentially, global variables. All instances of the class share the same static variable.
Important points for static variables :-
- We can create static variables at class-level only. See here
- static block and static variables are executed in order they are present in a program.
Below is the java program to demonstrate that static block and static variables are executed in order they are present in a program.
from m1 Inside static block Value of a : 20 from main
When a method is declared with static keyword, it is known as static method. The most common example of a static method is main( ) method.As discussed above, Any static member can be accessed before any objects of its class are created, and without reference to any object.Methods declared as static have several restrictions:
- They can only directly call other static methods.
- They can only directly access static data.
- They cannot refer to this or super in any way.
Below is the java program to demonstrate restrictions on static methods.
When to use static variables and methods?
Use the static variable for the property that is common to all objects. For example, in class Student, all students shares the same college name. Use static methods for changing static variables.
Consider the following java program, that illustrate the use of static keyword with variables and methods.
name : Alice rollNo : 1 cllgName : XYZ name : Bob rollNo : 2 cllgName : XYZ
Static nested classes : We can not declare top-level class with a static modifier, but can declare nested classes as static. Such type of classes are called Nested static classes. For static nested class, see static nested class in java
This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Comparison of static keyword in C++ and Java
- Understanding "static" in "public static void main" in Java
- Understanding storage of static methods and static variables in Java
- Why non-static variable cannot be referenced from a static method in Java
- Static and non static blank final variables in Java
- Difference between static and non-static variables in Java
- Difference between static and non-static method in Java
- strictfp keyword in java
- Super Keyword in Java
- abstract keyword in java
- final keyword in java
- native keyword in java
- volatile keyword in Java
- return keyword in Java
- transient keyword in Java
- Static blocks in Java
- Static import in Java
- Static class in Java
- Initialize a static Map using Java 9 Map.of()
- Protected keyword in Java with examples