Static Method in Java | Static Binding vs Dynamic Binding
When we dig deep into Java, the static method is a vital topic to discuss. Binding in the forms of static and dynamic have a specific purpose of serving. This article will lay the foundation for static methods in Java and the types of binding.
Static method in Java:
We already knew that ‘static’ is a keyword in Java. So, applying keywords with any method makes it a static method. This is a simple explanation for a static method; now, let’s dive deep into it.
- A static method usually belongs to the entire class rather than to the object of the class.
- This method can be invoked even without creating an instance for the class.
- It accesses only static data members or static methods and changes its value.
Why do we need Java static methods?
- Static methods are mostly to access static variables.
- It is helpful as we need not create a new object.
- We need static methods to perform calculations in input arguments and return the desired value.
- These methods are created when the object has no specific effect on its behavior as they depend entirely on its parameters.
- It is useful when the utility of the class should not change.
- Static methods play a major role when we don’t want the methods to be overridden.
- It can be used when the code is not dependent on the creation of an instance.
Sample program for static method in Java:
class FirstCode_Calc{ static int sq(int a){ return a*a; } public static void main(String args[]){ int ans = FirstCode_Calc(10); System.out.println("Area of the Square is: " +ans+ "sq.cm"); } }
Output:
Limitations for java static method:
- It cannot call a non-static member directly.
- It cannot implement non-static data members or variables.
- Classes like this and super cannot perform under static methods.
Static binding vs Dynamic Binding in java
What is Binding in Java?
Before getting into the topic of static and dynamic binding, let us know what binding basically means. In technical terms, the process of connecting a method call to the method body is binding.
It is further classified into two types:
- Static Binding – Early Binding
- Dynamic Binding – Late Binding
Types of instances in java:
Binding varies according to the type of instance.
1) Variables have a type:
Semantics to denote variables having a type:
int n = 5;
here, the variable n is of type int.
2) References have a type:
Sample program to denote references having a type:
class FirstCode{ public static void main (String args[]){ FirstCode fc; // here, the reference fc is of type FirstCode } }
3) Objects have a type:
Sample program to denote objects having a type:
class FirstCode{ class FirstCode2 extends FirstCode { public static void main(String args[]) FirstCode2 fc2 = new FirstCode2(); } }
Here, fc2 is an instance of the FirstCode2 class and also an instance of the FirstCode class.
Static Binding in java:
If the object’s type is determined during the compile-time, it is known as static binding or early binding. Here, the compiler will be able to resolve the binding during the compile time. Static binding, private and final methods are likely to resolve during compile time. This is because overriding cannot take place in these three methods. Therefore, overloading methods can take place.
Sample program for depicting static binding in Java:
class ComputerScience { public static void learn() { System.out.println("Learn Computer Science via FirstCode"); } } class Java extends ComputerScience{ public static void learn("Learn Java via FirstCode"); } public static void main(String args[]) { ComputerScience cs = new Java(); ComputerScience cs2 = new ComputerScience(); } cs.learn(); cs2.learn(); } }
Output:
Learn Computer Science via FirstCode
Explanation:
The two classes, ComputerScience and Java, contain the same method learn(). This method is static and cannot be overridden. Even though the Java class (cs2) object is used, the parent class ComputerScience is called by it. Therefore, the binding of static, private, and final methods takes place during the compile time as decided by the compiler.
Dynamic Binding in java:
If the object’s type is determined during the run-time, it is known as dynamic binding. Overriding takes place as the methods are not static, private, or final.
Sample program for depicting dynamic binding:
class ComputerScience { public void learn() { System.out.println("Learn Computer Science via FirstCode"); } } class Java extends ComputerScience{ public void learn("Learn Java via FirstCode"); } public static void main(String args[]) { ComputerScience cs = new Java(); ComputerScience cs2 = new ComputerScience(); } cs.learn(); cs2.learn(); } }
Output:
Learn Computer Science via FirstCode
Explanation:
In this example, the output varies from that of the static binding example. Here, the type of object(cs) is denoted as Java. Therefore, the method of the Java class is called first. This is determined during the run time.
Differences between static binding vs dynamic binding:
Static Binding | Dynamic Binding |
It occurs during compile time; also known as early binding. | It occurs during run time; also known as late binding. |
It uses overloading methods. | This uses overriding methods. |
It occurs during normal functions. | It occurs during virtual functions. |
Real objects do not prefer this method. | Real objects mostly prefer this method. |
Conclusion:
These are the differences between static and dynamic binding. To put it short, the static binding takes place at compile-time and the latter during runtime. Binding the overloaded methods are static, and binding the overridden is dynamic. You can now execute them in your programs to acquire the desired result.