Java static variable in Method and Class with example

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.

Java static variable in Method and Class

Static variable Syntax

 Naming syntax is the same as instance and local variables. Static keyword followed by data type. Then 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.

Now call the “name” static variable form another class “Company” without create class object.

Output: John

Another example to see how static variable works:-

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:-

Output: Error:(6, 9) java: illegal start of expression

Static variable in method

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:-

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:

  • longshort etc : 0 (primitive integers)
  • floatdouble: 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 provide ClassName.staticMemberName only when your are referring it from outside the class.

So, your statement should be like:-

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:

Next you can refer to Math.PI simply by using PI. For example:

Second, as long as you are in the scope of the class, all static members can directly be addressed without having to qualify.

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 examples are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.