← Back to list

Static: A keyword that you must know to clear your Java interviews

you are ec-static if you know it, pathetic if not!

Dozerex · 2026-04-01 03:37 · 55 claps · 3.1 min read
#java #spring-boot #static #code-analysis #java8
Open on Medium ↗

Static: A keyword that you must know to clear your Java interviews

you are ec-static if you know it, pathetic if not!

-Dozerex

If you have worked with Java programs then you must come across ‘static’, I bet. Like I bet real money on this one, because Standalone JVM applications require a public static void main entry point. Why is that? we will see soon…

this is Dr.Steven Static, not Strange

this is Dr.Steven Static, not Strange

Remember typing…

public static void main(String[] args){
...
}

Yeah! right there! did you see ‘static’. What’s this supposed to mean? Why do we see it everywhere, what is it capable of? we will see it all in this blog

Static: the Keyword

The static keyword tells the compiler that the access of the mentioned resource should be classified as common across the class and should be initialized only once per class loader. It is the class that should be used to access this resource not the instance(i.e the object of the class).

Where all can we use: static

TBH, many use cases area possible. I can give you my three point checklist for this one actually:

  1. If the value’s requirement is class level/is used as a common value across all the objects.
  2. If the method should be callable, even without the need of an object of the class.
  3. If some code should run only once when the class is initialized.

If either of these rules come true, static keyword can come of use. But this actually leads the way to explain the type of usage of the static keyword, I will list them below and explain them one by one.

Static variables

public static int count;

Such variables are created per-class basis, whenever we need a common variable across all the objects created under the class. Like a counter for the number of times a function is called, a lock for a file that is being accessed(not the most efficient way, I believe, but still..), and a lot more cases that you can think of. Static variables are initialized once and a updated every time it is called across the objects of the class.

Static function

public static void main(String[] args){

AYE! AYE! AYE! A simple feature, but I took a complex example to explain my case here. Static methods are common & will remain under the ownership of a class. We can call these methods without creating an object of the class. These are basically class level functions. Main function has to be static for a reason, why bother creating an object of you class to start your program. Wait! will that be a paradox?

Static Initializer

static {
System.out.println("Bro! Somebody initialized your class);
}

This is the reason that led me to writing this blog. A static initializer code block that is present inside a class will run only once. That is when the class is initialized for the first time. After that? never again! These kind of implementation can be used when you need to run a function to do something for once an only once while using this class.

Static Imports(Even I was shocked!)

import static java.util.concurrent.TimeUnit.SECONDS;

Thread.sleep(5 * SECONDS.toMillis(1));

When you write a simple import statement, you are importing the class. Whenever requried, you call the functions of the class by doing Class.Function().

To help improve readability, you can do a static import, and here is how it works.

When you do a static import, it basically imports the static methods mentioned in the import statement. Now, instead of doing Class.function(), you can just call it by function() (You should have ‘import static com.package.Class.function;’ in you import statements). This helps readability. But it is recommended not to use it when you have ambiguous functions like sort and stuff… Just a warning!

Static methods, but in Interfaces

Did you know you can define static functions inside an interface? I was forced to believe that you only create abstract methods inside and interface but no! You can create a lot more, like static and default methods. Let’s just focus on static for now.

When you define a static method in an interface you also have to implement it in the interface (i.e. write the body that will be executed when this function is called), and it will called from the Interface and not its instance. It cannot be overridden by a child class that implements the Interface. Pretty Interesting!

So I believe this was a good way to ‘public static void main’ your Java quest of learning the static keyword. This is good bye from Dozerex — Until next time!


메타데이터
post_id
c71c8d050b47
slug
static-a-keyword-that-you-must-know-to-clear-your-java-interviews-c71c8d050b47
url
https://medium.com/@dozerex-codes/static-a-keyword-that-you-must-know-to-clear-your-java-interviews-c71c8d050b47
canonical_url
https://medium.com/@dozerex-codes/static-a-keyword-that-you-must-know-to-clear-your-java-interviews-c71c8d050b47
author_url
https://medium.com/@dozerex-codes
status
ok
fetched_at
2026-07-14 10:49:49