×

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

Java HashSet

HashSet implements the set interface. It uses the hash table to make the collection to store different data types. The hash set is the unordered collection of different data types....

6 minutes read.

How to Convert Date to String in Java

How to Convert Date to String in Java We need to convert Date to String in Java may for displaying purpose. We can convert Date to String in Java using the format() method of java.text.DateFormat class. There...

2 minutes read.

Internal Working of ArrayList in Java

Java's version of a resizable array is called ArrayList. The dynamic growth of an array list ensures that there is always room for new elements. ArrayList's backing data structure is...

8 minutes read.

Class vs Object in Java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Java Location

PATH is an environmental variable that the system software now uses to find executable files (.exe) or Java binaries ( java or javac command). Once chosen, a route cannot be...

3 minutes read.

Java Class Keyword

We know that java is object-oriented programming language which contains the essential key concepts such as classes and objects etc to have clear idea about the object-oriented programming.Java is mainly...

3 minutes read.

Java Enumeration

In a computer language, enumerations express a set of named constants. For instance, the four suits in a deck of playing cards could be represented by the enumerators Club, Diamond,...

4 minutes read.

User Defined Exception in Java

An exception is an error (run time error) that happened while a program was being executed. The program stops abruptly whenever the Exception occurs, and the code after the line...

3 minutes read.

Hashing Algorithm in Java

The hashing algorithm is a method that maps data to the fixed-length hash. The Java hash-based algorithm employs a cryptographic mathematical operation. A hash technique or hash function is supposed...

8 minutes read.

How to Solve the Deprecated Error in Java?

Deprecated Java methods should not be used because they are deprecated (often, there are better, more modern alternatives). API update. Until now, Java has kept everything backward compatible and never...

3 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.

Display List of TimeZone with GMT and UTC in Java

It is vital to establish the right TimeZone in Java code when working with dates for Daylight Saving Time. In this part, we will present the time zones with GMT. TimeZone Those...

5 minutes read.

Java Vs C++

Java Vs C++ Java and C++ both are Object Oriented Programming languages. Both languages are popular for competitive programming. C++ is used by many coders who have just started learning programming...

4 minutes read.

Java float vs double

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Binary Search Java

Binary search is a search mechanism for key elements from the given List/Array. In Binary search, the search mechanism is followed by dividing the array into parts; hence the search...

3 minutes read.

Buffer reader to read string in Java

The Buffered Reader class of Java is used to read the stream of characters from the input stream. Program to read string using Buffer reader import java.io.*; class  Demo {   public static void main(String...

3 minutes read.

Java String subSequence() method

This method returns a new character sequence i.e. subsequence of current sequence Syntax: public CharSequence subSequence(int beginIndex, int endIndex) Parameter: beginIndex ? begin index, inclusive. endIndex ? end index, exclusive. Return: specified subsequnce Throws: It throws IndexOutOfBoundsException...

1 minute read.

Java Math hypot() Method

The hypot() method of Math class returns the square root for the expression x2 + y2 without the intermediate underflow or overflow . Syntax: public static double hypot(double x, double y) Parameters: The parameters...

2 minutes read.

Brilliant Number in Java

It is a number N that is made up of two prime numbers that have the same number of digits and is called a brilliant number. Several/Some of the brilliant Numbers...

3 minutes read.

Heap Sort in Java

Heap Sort in JavaHeap sort in Java uses the data structure binary heap, min-heap, or max heap to do the sorting of elements. Since min-heap always gives the minimum element first,...

8 minutes read.