Java primarily uses the “static” keyword for memory management. Variables, blocks, and methods all can be used with the static keyword. Instead of a particular class instance, the static keyword refers to the class as a whole. Without the static keyword, in Java, the methods and variables are by default considered “non-static”.
This article will explain the static and non-static in Java.
What is static in Java?
The “static” is a keyword in Java utilized for memory management of the variables and methods that belong to a class. In Java, there are static variables and static methods in the classes, which will be discussed in the below sections.
How to Create static Variables in Java?
The static variables are class variables and can be created at the class level. When a variable is declared as “static”, the compiler creates a copy of the variable and shares it with all objects of the class. On the other hand, static variables effectively function as global variables. It can be retrieved by the class name alone and does not need to be used with an object. It saves memory and enhances the software’s memory efficiency.
Syntax
For creating the static variable in a class, use the below syntax:
Here, “x” is the static variable that stores the integer type value “10”.
Let’s discuss what a static method in Java is and how to create it.
How to Create static Method in Java?
A static class method can be accessed without the creation of an object or instance of a defined class. It can retrieve only static data members and methods of the same class or another class; they cannot access non-static methods or variables.
Additionally, any static data member’s values can be changed by a static method.
Syntax
Follow the given syntax for creating a static method in Java:
//body of method
}
The syntax below indicates how to call the static method:
Note: For calling the static method, we will use the class name. We do not need the class object for calling the method because the static keyword fixes a specific memory location for that method in RAM, and the memory allocation only occurs once in the static method.
Example
We will create a static method named “display()” in the “Message” class and invoke it in another class:
public static void display() {
System.out.println("Welcome To LinuxHint");
}
}
Here, in the main() method of another class named “Example”, we will call the static method display() without creating an instance or object of the “Message” class:
public static void main(String[] args) {
Message.display();
}
}
The output shows that we have successfully accessed the static method of the class in another class:
Let’s move to non-static in Java.
What is non-static in Java?
Every method and variable in Java without the “static” keyword is “non-static”, and it can access all static methods and variables. The non-static keyword use runtime binding, also known as dynamic binding.
How to Create non-static Variables in Java?
A “non-static” variable is a class variable that is not static. The non-static variables are also called local variables. It is defined inside a method, constructor, or block. The scope of these variables is restricted. That’s why we can only access these variables within the defined block.
Syntax
Use the given syntax for creating a non-static variable in Java:
Here, “x” is the local variable called a non-static variable that stores the integer type value “10”.
Let’s see what a non-static method is in Java and what is the process of creation.
How to Create non-static Method in Java?
Java permits you to override the non-static method. This type of method is created using the keyword “non-static”. In non-static methods, dynamic and runtime binding is utilized. As a result, we cannot call it without the class’s instance. In this case, memory is allocated whenever the method is called in the non-static method, and it happens each time.
Syntax
Follow the given syntax for the non-static method:
//body of method
}
To invoke the non-static method in Java, we will first need to create an object of the class and then call it.
Let’s move to the example to see how to access a non-static method in Java.
Example
Here, we will access the non-static method of a class in another class by creating an object of the class. To do so, first, we will create a non-static method named “display()” that will print the statement using the “System.out.println()” method in a “Message” class:
public void display() {
System.out.println("Welcome To LinuxHint");
}
}
Now, we will create an object “msg” of the Message class in the main() method of the other class named “Example”, then, we will call the non-static display() method with it, because the non-static methods are not accessed without the object of the class:
public static void main(String[] args) {
Message msg = new Message();
msg.display();
}
}
Output
Let’s move towards the key difference points between a static method and non-static method in Java.
Difference Between static and non-static Methods in Java
static Method |
non-static Method |
It is declared with the “static” keyword. | No need to declare with any keyword. |
It is a member of a class but is not a member of an instance of that class. | It belongs to a class and is both a member and an instance of that class. |
Only static methods and data members from the same class or another class may be accessed by it. | It can access static and non-static data members and methods of the class. |
Memory allocation happens only once. | When the method is called, memory is allocated. |
No object needs to be created in order to access it. | To access it, you must create a class object. |
It uses compile-time or early binding. | It uses dynamic or runtime binding. |
It cannot be overridden. | It can be overridden. |
We have included all of the guidance for Java’s static and non-static variables and methods.
Conclusion
In Java, the variables and methods are non-static by default, but the main() method is static. For managing memory, you can utilize the static keyword. The static methods can be made accessible without creating a class object, in contrast to the non-static methods, which cannot be accessed without creating an object. In this article, we explained the static and non-static variables and methods with detailed explanations and examples.