Open In App

Dynamic Class Data Sharing in Java with Example

Last Updated : 31 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Java, a class is a template that defines the properties and behaviors of objects. Objects are instances of a class and can be created using the new keyword. A class typically consists of fields (also known as instance variables) and methods. Fields are variables that store data, and methods are blocks of code that perform a specific task. A class can also have constructors, which are special methods that are used to create objects of the class.

Class data sharing refers to the ability to share data between different instances of a class or between different classes in a program. In Java, you can share data between classes using static fields and methods, global variables, or by using objects such as java.util.Map. java.util.Map is an interface in the Java Standard Library that defines a data structure for storing key-value pairs. The Map interface has several implementations, such as java.util.HashMap, which allows you to store data in a key-value format and retrieve it using the key. Maps are useful for storing and sharing data dynamically between different parts of a program.

To share class data dynamically in Java, you can use the java.util.Map interface and its implementations, such as java.util.HashMap. A Map is an object that stores key-value pairs and allows you to retrieve the value associated with a given key. You can use a Map to store data that needs to be shared dynamically between different classes in your program. Here is an example of how to use a Map to share class data dynamically in Java.

Example:

Java




import java.util.HashMap;
import java.util.Map;
  
public class DynamicClassDataSharingExample {
    // Create a Map to store the data
    private static Map<String, Object> data
        = new HashMap<>();
  
    // Add a public method that allows other classes to
    // access the data
    public static Map<String, Object> getData()
    {
        return data;
    }
  
    public static void main(String[] args)
    {
        // Add some data to the Map
        data.put("key1", "value1");
        data.put("key2", 123);
        data.put("key3", true);
  
        // Retrieve the data from the Map in another class
        AnotherClass.retrieveData();
    }
}
  
class AnotherClass {
    public static void retrieveData()
    {
        // Retrieve the data from the Map
        Map<String, Object> data
            = DynamicClassDataSharingExample.getData();
        String value1 = (String)data.get("key1");
        int value2 = (int)data.get("key2");
        boolean value3 = (boolean)data.get("key3");
        // Print the values of the data
        System.out.println(value1);
        System.out.println(value2);
        System.out.println(value3);
    }
}


In this example, we use a Map to store data with dynamic keys (strings in this case) and values of different types (string, integer, and boolean). The Map is defined as a static field in the DynamicClassDataSharingExample class, so it can be accessed from any other class in the program.

You can then retrieve the data from the Map using the name of the class that contains the Map followed by the name of the Map and the corresponding key. Note that you need to cast the values retrieved from the Map to the appropriate type. The Map interface stores the values as objects, so you need to cast them to the correct type before you can use them.

Output:

value1
123
true


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads