Which Java variable is declared with “static keyword” called a Java static variable. A static Variable is a class-level variable. It’s initialized at class load and a single copy has been 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
The naming syntax is the same as instance and local variables. Static keyword followed by data type. Then Variable name.
static data variable_name;
Java static variable example
You can use static variables at the class level. Let’s see the example we declared “name” static variable in Employee class.
public class Employee { public static String name = "John"; }
Now call the “name” static variable from another class “Company” without creating a class object.
public class Company { public static void main(String[] args) { System.out.println(Emplyoee.name); } }
Output: John
Another example to see how static variable works:-
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 variables inside of the 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 keywords can be used only in a ‘class scope‘.
Let’s try it and see what happens:-
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 a static keyword makes 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:-
class Test { final static int x = 20; public static void main(String[] args) { System.out.println(x); } }
Output: 20
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 the class name.
You need to ClassName.staticMemberName
So, your statement should be like:-
System.out.println(st.email);
Q: How can we access the static variable without a class name?
Answer: Static variables are always qualified with the 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:
import static java.lang.Math.PI;
Next, you can refer to Math.PI
simply by using PI
. For example:
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.
public class Test { public static int static_member; //within this scope you can call static_member without Test. }
Do comment if you have any doubts and suggestions 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.