Which Java variable is declared with “static keyword” called a Java static variable. Static Variable is a class-level variable. It’s initialized at class load and a single copy has created and shared among all the instances of the class.
Static Variables are good for memory management. It’s created when the program starts and destroyed when the program stops.

Static variable Syntax
Naming syntax is the same as instance and local variables. Static keyword followed by data type. Then Variable name.
1 |
static data variable_name; |
Java static variable example
You can use static variable in class level. Let’s see the example we declared “name” static variable in Employee class.
1 2 3 |
public class Employee { public static String name = "John"; } |
Now call the “name” static variable form another class “Company” without create class object.
1 2 3 4 5 6 7 |
public class Company { public static void main(String[] args) { System.out.println(Emplyoee.name); } } |
Output: John
Another example to see how static variable works:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public class Example { public static void main(String args[]) { Student s1 = new Student(); s1.showData(); Student s2 = new Student(); s2.showData(); } } class Student { int a; //0 static int b; //0 Student() { //Constructor incrementing static variable b b++; } public void showData() { System.out.println("Value of a = " + a); System.out.println("Value of static b = " + b); } } |
Output: Value of a = 0
Value of static b = 1
Value of a = 0
Value of static b = 2
Static variable in The method
or
“How to create Constants in Java using the Static Keyword“?
Many beginners are Unable to declare static variable inside of static method.
But why can’t we declare a static variable inside a static function?
Answer: In Java, static means that it’s a variable of a class, it belongs to the whole class but not to one of its certain objects. It means that static keyword can be used only in a ‘class scope‘.
Let’s try it and see what happens:-
1 2 3 4 5 6 7 8 |
public class Example { // Top level class public static void main() { static int number = 10; } } |
Output: Error:(6, 9) java: illegal start of expression

What are Static final variables
A using a final keyword with static keyword make Java Variables constant. Important point any final variable always needs initialization, otherwise, it will throw a compilation error.
Constant variables never change from their initial value.
Example:-
1 2 3 4 5 6 7 |
class Test { final static int x = 20; public static void main(String[] args) { System.out.println(x); } } |
Output: 20
Interview questions & Answers
Q:What is the default value of the static variable in java?
Answer: Default value of static is the same as non-static variables:
long
,short
etc : 0 (primitive integers)float
,double
: 0.0 (primitive floating points)- boolean: false
- object references: null
Q: How to call static variable?
Answer: You can call the static variable followed by class name.
You need to ClassName.staticMemberName
So, your statement should be like:-
1 |
System.out.println(st.email); |
Q: How can we access the static variable without a class name?
Answer: Static variable are always qualified with class name.
First of all, it is not true you have to qualify with a class name, you can use a static import for instance:
1 |
import static java.lang.Math.PI; |
Next you can refer to Math.PI
simply by using PI
. For example:
1 2 3 4 5 6 7 8 9 |
import static java.lang.Math.PI; public class Test { public static void main (String[] args) { System.out.println(PI); } } |
Second, as long as you are in the scope of the class, all static members can directly be addressed without having to qualify.
1 2 3 4 5 6 7 |
public class Test { public static int static_member; //within this scope you can call static_member without Test. } |
Do comment if you have any doubt and suggestion on this tutorial.
Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1
Java version 11
All Java static keyword examplesare in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.