Home
Time Box
Calculator
Snake
Blogs
Hacks

Static? Why use it • 11 min read


Static. This is a keyword you come across a lot when developing using Java. Even if you just look at the main function,

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

So, let’s find out why static is attached, what it is, and how it is used.

What Static?

Static means ‘fixed’. The reason for having this name is because variables or methods with static in front of them are variables or methods that are fixed to the class, not belonging to any object.

Let’s look at the features of Static

  1. Statically allocated in memory.
  2. Can be used without creating an object.
  3. When the program starts, it is loaded into the static area of ​​memory and is released when the program ends.
  4. Instance variables cannot be used within static methods.

1. Statically allocated in memory.

Methods or variables that are not Static are called each time an object is created and may have different values. Therefore, static can be useful when one value must be maintained among each object.

2. Can be used without creating an object.

Belonging to an object means that the variable or method can only be used by creating the object using the new keyword.

Let’s look at the code

public class Main {
 
    public void print(){
        System.out.print("Is this able to run?");
    }
 
    public static void main(String[] args){
        print();
    }
}

Unfortunately it doesn’t work and a compilation error occurs. The reason is that the method called print is not a static method, so it can be executed only when an object called Main is created. In order to run the code, it should be like this:

public class Main {
 
    public void print(){
        System.out.print("Is this able to run?");
    }
 
    public static void main(String[] args){
        Main main = new Main();
        main.print();
    }
}
Main.main(null);
Is this able to run?

However, if the static is added in front, the code can be different.

public class Main {
 
    public static void print(){
        System.out.print("Is this able to run?");
    }
 
    public static void main(String[] args){
        print();
    }
}
Main.main(null);
Is this able to run?

By adding the static, you can use a method or variable without creating an object.

3. When a program starts, it is loaded into the static area of memory and is released when the program ends.

When a program starts and a class is loaded into memory, static variables or methods are automatically created in the static area of memory along with the class. Since it is automatically loaded into memory, it can be used without creating an object. Also, since it is automatically loaded into memory, the main function can be implemented as static so that it can be executed immediately.

When a general method creates an object, it goes up to the heap area of memory. This heap area of memory is automatically managed by the Garbage Collector. In other words, it manages memory by automatically deleting unused objects.

On the other hand, static methods do not receive this management because they exist in the static area. Therefore, memory is released when the program ends, which can lead to memory overload if too many statics are declared.

Also, if there are more static methods than the memory size of the static area when the program starts, an error may occur at the start. Therefore, you should be careful about indiscriminately using static just because it is easy to use.

4. Instance variables cannot be used within static methods.

Static methods are loaded into memory as soon as the program is executed, so instance variables cannot be used. Since instance variables can only be used by creating an object, they cannot be used in static methods that are loaded into memory before creating an object. So, if you want to add static to a method, you must first check whether there is a part inside the method that uses an instance variable or an instance method.

Of course, on the contrary, using static variables in instance methods is not a problem at all.