×

Static and Dynamic Binding in Java

Data Binding in Java

The relation between a class and method or class and field is known as Data Binding. Static and Dynamic Binding in Java- TutorialandExample.com In Java, we can create a class for Student_info, but until we link up the class with variables, methods, and constructors, it is just a class. Let's say we add variables for student_name, course, age, etc. when we create a new instance to the Student_info class and access the class variable, these items are bound together. This process is called Data Binding.
public class Student_info
{
public static int roll_no;
public static string name;
public static string course;
}
Now we can create the objects and instances of Student_info and use these
variables and methods.
Student_info s1=new Student_info();
Student_info s2=new Student_info();
s1.roll_no=105;
s2.roll_no=106;

Types of Data Binding in Java

There are two types of Data Binding.
  • Static Binding
  • Dynamic Binding

Static Binding in Java

It's a compile-time Binding. When we call a method, Java binds fields and method with classes at the compile time. It can be also called as the Early Binding. Let's look at our Student_info class again.
public class Student_info
{
public static String name;
public static String course;
public static String Info( String n, String c)
{
name=n;
course=c;
return c;
}
public static void main(String[] args)
{
     Student_info s1= new Student_info();
     s1.Info("Alex", "B.tech");
     System.out.println("Info: "+ name+ " " + course);
}}
Output
Info: Alex B.tech
Because the method Info() is static, it means the method belongs to class itself. If we create many instances for Student_info, we only reference the fields and methods for one time. So if we declare a method with keyword static, final or private, binding is going to be static.

Dynamic Binding in Java

It's a runtime Binding. It occurs at runtime because Java would not know which method or field to reference at compile time. In Dynamic binding, the type of the object is determined at run time. One perfect example of dynamic binding is method overriding as in overriding both parent and child classes have the same method, and in this case, the object type determines which method is to be executed. The object type determined at the run time so this is known as dynamic binding. It can also be called as the Late Binding. Let's have a look at the below example
class College_info{ 
      void info(){
           System.out.println("Getting college Info...");} 
     } 
      
     class Student_info extends College_info{ 
      void info(){
           System.out.println("Getting Students Info...");} 
      public static void main(String args[]){ 
       College_info c=new Student_info(); 
       c.info(); 
      } 
     } 
Output
Getting Students Info...
In the above example, the compiler cannot determine the object type, because the instance of Student_info is also an instance of College_info.  So compiler does not know its base type.

Related Topics

Figurate Number in Java

There have been several uses for figurate or figural numerals throughout history. A number that may be expressed by regular, distinct geometric shapes with spaced evenly points is referred to...

4 minutes read.

Minimum Number of Platforms Required for a Railway Station

The train station problem is one of the most significant problems typically posed in the programming round interview to gauge a candidate's aptitude for logic and problem-solving. Problem of Railway Station The...

6 minutes read.

URLConnection Class

What is the URL? URL stands for Uniform Resource Locator, is used to specify addresses on the World Wide Web. A URL relates to the identification of any resource connected to the web. URL syntax: Protocol://hostname/other_information(files...

6 minutes read.

Java gc()

Garbage collection Java language provides different ways to perform the task of memory management. In Java, objects are declared and assigned references. Once lifeof an object is completed it is dereferenced...

4 minutes read.

Array and String based questions in Java

1. What is an Array in Java? A collection of identical data types is referred to as an array. There can be no separate data kinds. It supports the storage of...

4 minutes read.

Java FileNotFoundException

FileNotFoundException is another exception class accessible in the java.io bundle. The exemption happens when we attempt to get to that document which isn't accessible in the framework. It is a checked...

5 minutes read.

Java Math signum() Method

The signum() method of Java Math class returns the signum function of the value. Syntax: public static double signum(double d)public static float signum (float d) Parameters: The parameter ‘d’ represents the floating-point value whose...

2 minutes read.

Difference between Aggregation and Composition in Java

What is Aggregation? Before we start discussing Aggregation, we have to know some general information about our classes in java. Generally we have two members in our classes , the first...

8 minutes read.

How to add 24 Hours to Date in Java?

In this tutorial, we will learn how to add 24 hours to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Java ArraylistRemove() Time Complexity

In this tutorial, we will learn about how we can remove time complexity in Java ArrayList. But unless we will not learn what is ArrayList, we don’t understand this process....

7 minutes read.

Type Annotations in Java

Only declarations were eligible for annotations in earlier versions of Java. With Java 8, you may now annotate any type use, including types in declarations, generics, and casts: @Encrypted String data; List<@NonNull...

6 minutes read.

Delete a Cycle from a Linked List in Java

Assume a method detectAndRemoveLoop(), which examines if a given Linked List includes a loop and, if so, eliminates this Loop and returns true. This returns false if the list does...

7 minutes read.

Java For Loop

A for loop is used to execute a set of statements for a fixed number of times. It takes the following form: for (initialization; condition; update) { statements; } The for loop defines...

2 minutes read.

Java Calculate Average of List

The list is a linear data structure used in Java to store ordered data collections. Additionally, it accepts duplicate values while maintaining insertion order. It is sometimes necessary to find...

3 minutes read.

Sort Dates in Java

The Collection class's sort() method, which is a component of the Comparator mechanism, arranges the data in decreasing order. The Comparator interface can be used to accomplish the goal while...

4 minutes read.

Ternary Operator in Java

In some cases, the if...else expression in Java can be replaced by a ternary operator. Visit the Java if...else statement first before learning about the ternary operator. Ternary operator in Java A...

3 minutes read.

Types of Sockets in Java

The fundamental idea behind Java's networking capability is the socket. Early in the 1980s, the Berkeley UNIX 4.2BSD version included the socket paradigm. Berkeley socket is the term employed as...

9 minutes read.

Java Set Interface

We use set when we don't want to allow duplicate entries. All Set implementations do not allow duplicates. HashSet: This class stores its elements in hash tables. It uses the hashCode()...

3 minutes read.

Tribonacci series in Java

The Fibonacci series and the Tribonacci sequence are linked. The Fibonacci sequence, each element summates the three preceding terms, is expanded into the Tribonacci series in Java. Through specific examples,...

3 minutes read.

Program to Reverse a Number in Java

In order to reverse a number, the digit in the first place must be swapped with the digit in the final position, the second digit with the second-to-last digit, and...

3 minutes read.