In this article, a static map is created and initialised in Java.
A static map is a map which is defined as static. It means that the map becomes a class member and can be easily used using class.
- Method 1:
- Creating a static map variable.
- Instantiating it in a static block.
Below is the implementation of the above approach:
// Java program to create a static mapimportjava.util.*;classGFG {// Declaring the static mapprivatestaticMap<Integer, String> map;// Instantiating the static mapstatic{map =newHashMap<>();map.put(1,"GFG");map.put(2,"Geek");map.put(3,"GeeksForGeeks");}// Driver codepublicstaticvoidmain(String[] args){System.out.println(map);}}chevron_rightfilter_noneOutput:{1=GFG, 2=Geek, 3=GeeksForGeeks} - Method 2: Creating a static map variable and instantiating it together.
Below is the implementation of the above approach:
// Java program to create a static mapimportjava.util.*;classGFG {// Declaring the static mapprivatestaticMap<Integer, String> map=newHashMap<>() {map.put(1,"GFG");map.put(2,"Geek");map.put(3,"GeeksForGeeks");}// Driver codepublicstaticvoidmain(String[] args){System.out.println(map);}}chevron_rightfilter_noneOutput:{1=GFG, 2=Geek, 3=GeeksForGeeks}
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Initialize a static Map using Java 9 Map.of()
- Initialize a static Map using Stream in Java
- Initialize a static Map in Java using Double Brace Initialization
- Internal static variable vs. External static variable with Examples in C
- Static and non static blank final variables in Java
- Understanding "static" in "public static void main" in Java
- Difference between static and non-static method in Java
- Difference between static and non-static variables 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
- KeyPairGenerator initialize() method in Java with Examples
- How to Initialize and Compare Strings in Java?
- Initialize an ArrayList in Java
- Initialize HashMap in Java
- Initialize a list in a single line with a specified value using Java Stream
- Initialize a list in a single line with a specified value
- Static Variables in Java with Examples
- Static and Dynamic data structures in Java with Examples
- Shadowing of static functions in Java
- Are static local variables allowed in Java?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

