Static Method in Java With Examples
The static methods and members are the members of the class. The static method in java and members in java can be accessed without creating the object of the class. They are used to represent the state of the class rather than the state of an object.
We cannot declare the static methods and members inside the body of any kind method of the class, and represented by the prefix static keyword in the class.
Introduction to Static Members in Java
Let's understand static methods and variables with the help of a real-world example. Let's suppose you want to design a class 'Student' that contains all the necessary information like name, age, class, school name, and rollno about the particular student that is studying in the school. Every object of the class represents about the student, Now think which information is common in between of all the students ?. The answer is School name.
In this case, we can consider schoolname as a static variable. Because static members are equally shared among all the objects of the class. Now you are thinking we can hardcore the schoolname in the class. Yes ! we can but this is not a good way because the memory consumption in more in this case due to individual memory allocation of the schoolname variable for every object in the heap memory.
if you are confused between a static method in java and instance method, always remember one rule "Does it make sense when you call a particular method without creating the object of the class ". If yes, then the method should be static. You are very familiar with one of the most widely used static method in java i.e the main() method of every Java program.
The main() method is of static type because when java runtime starts, there is no object of the class present. That's why the main method has to be static, so JVM can load the class into memory and call the main method.
Let's understand the structures of the static method in java and the static variable java.
What is Static Method in Java?
The static methods are the class methods and they don't belong to instances of the class. These are designed to be shared among all the instances of the same class and they are accessed by the class name of the particular class. A static keyword is used for declaring the static method in java
The static method in java cannot be overridden because static methods are linked to the class during the compilation time and we know very well that method overriding is based on the runtime polymorphism that's why the static methods cannot be overridden.
The static methods can be accessed by the outside environment very easily. We have already dicussed the main() method example in the above section.
Features of Static Method in Java
- Rather than being an instance of a class, a static method in Java is a method that is a part of the class itself.
- The method is available to each instance of the class.
- The method is available to each instance of the class. Without requiring the class's object, static methods have access to class variables (static variables) (instance).
- A static method can only access static data. It cannot access data that is dynamic (instance variables).
- Static methods can be accessed directly in non-static methods as well as static methods.
Let's Understand the Syntax of the Static Method in Java
The return_type of the static method can be int, float etc any user-defined datatype.
Note: if we want to make any method to be static, we have to use a keyword i.e static before the declaration of the method in the class. Let's understand how to declare a static method in java.
Declaration of Static Method in Java
The declaration of the static method is similar to the declaration of the instance method but in the case of static method declaration, we have to use a static keyword before the declaration of a method.
Example for declaration of static method in Java:
The print() method of the class Scaler in of static type and we don't require an instance of the class for accessing it. Let's understand how to call static methods.
Example: How to Call Static Methods
We can call the static methods with the help of the class name because they belong to the class itself and they have no relation with the instances of the class.
The classname is the name of the class followed by the name of the static method.
Let's understand how to call a static method using an example.
When to Use Static Method and Common Use for Static Methods
This question always arises in the mind of the programmer when to use a static method in the java programs. We can use the static method only in our class, when we know the proper use of the static method. Let's understand some common use for static methods so that we can efficiently use them.
- When the implementation of the particular method is not dependent on the instance variables and instance methods, In this case we can make that method to be static.
- If the implementation of the code of the method is not changed by the instances of the class. In this case, we can make the method a static method.
- If we want to define some utility functions such as user defined sorting method in the class, we can define them using static concepts. Because the utility function can easily be shared among all objects of the class due to the property of the static method.
Main Method in Java Static
Java's main() method is static since an object doesn't need to call a static method. If the function weren't static, JVM would create an object first before executing the main() method, which would make memory allocation more complex.
Static Method in Java Examples
After talking about static methods and when you might use them, let's look at some real-world examples to understand how they function.
Example 1: Creating a Utility Class
Let's say we want to develop a utility class that includes operations for performing math. To include static methods for addition, subtraction, multiplication, and division, we may make a class called MathUtils. This is how the MathUtils class might appear:
We have developed a utility class with four static methods for carrying out mathematical operations in the code above. These MathUtils class methods can be called without first constructing an object of that class. For instance:
Example 2: Implementing the Singleton Design Pattern
For managing resources like database connections or thread pools, singletons are frequently utilised. Suppose our goal is to develop a class that symbolises a database connection. We can use the singleton design pattern to ensure that there can only be one connection to the database at a time.
Given below is the Java implementation to demonstrate the above explanation:
The DBConnection class's constructor has been designated as private in the code above. By doing this, the DBConnection class cannot be instantiated by other classes. Moreover, a static method for obtaining the class instance and a static instance of the class are declared.
First, the getInstance() method verifies whether a class instance has already been created. If not, it builds a class instance and returns it. If not, it returns the current instance. This guarantees that only one instance of the DBConnection class can exist at any one moment.
Restrictions of Static Method in Java
When the methods are declared as static, there are lots of restrictions applied on the static method that makes it different from the instance methods of the class.
Let's discuss some restrictions of the static method in java.
- The static method cannot invoke the instance member as well as methods of the class. Because static methods are accessed without the object reference of the class but we cannot access the instance variables and method without an object reference. This will cause an error if we try to access the instance variable or methods inside the static method.
Let's try to understand this using an example.
In the above program, we are trying to access the nonstatic method from the static method of the class which will cause an error. Use this Free Online Compiler to compile your Java code.
- The static methods can only access only other static members and methods. Because static members and methods are linked to the class during the compilation time that's why they can access each other without creating the instance of the class.
- We cannot use this and super keyword in the body of the static method because this keyword is the reference of the current object but without creating an object we can access the static method this will cause an error and in the case of the super keyword, we know very well super is the reference of the parent class but we cannot use reference in the body of the static method. We have already discussed the reason behind this.
Important Points to Remember About Static Methods
- The static methods are the class methods and are accessed by the name of the class.
- The static methods cannot be overridden because static methods are resolved at the compilation time of the java program and we know very well the method overriding is the runtime polymorphism.
- The keywords such as super and this cannot be used in the body of the static method, we have already discussed the reason behind this in the above section.
- The static methods cannot access the instance members and methods of the class.
- The state of the static methods is equally shared among all the objects of the class.
Difference Between Static Method and Static Members in Java
|Property||Static method||static members|
|Classmember||The static methods are the class members and binds to the class during the compilation time.||The static members are also class members and binds to the class during the compilation time.|
|Defaultvalue||The static methods don't value default values.||The static members have default values like the default value of a static boolean member is false.|
|Overriding||Static methods don't follow the method overriding concept||static members don't follow the overriding concept as well.|
|Accessing||They are accessed by the name of the class.||They are also accessed by the name of the class.|
- The static members and methods belong to the class rather than the instance of the class.
- They are accessed by the name of the class.
- The keywords such as this and super are not used in the body of the static method.
- The modification on the static field value is not allowed.