ResourceBundle.clearCache(ClassLoader) in Java

clearCache(ClassLoader): This method is available in java.util.ResourceBundle class of Java.

Syntax:

 void java.util.ResourceBundle.clearCache(ClassLoader loader)

This method takes one argument. This method removes all resource bundles from the cache that have been loaded by the given class loader.

Parameters: One parameter is required for this method.

loader: the class loader.

Throws:

NullPointerException - if the loader is null.

Approach 1: When no exception

Java

import java.util.Locale;
import java.util.ResourceBundle;

public class ResourceBundleclearCache2 {
    public static void main(String[] args) {

        ResourceBundle bundle =
ResourceBundle.getBundle("Hello", Locale.US);

        String key = "Hello";
        System.out.println(bundle.getString(key));

        ResourceBundle.clearCache(new ClassLoader() {
        });

    }
}

Output:

Hello Java Program


Approach 2: NullPointerException 

Java

import java.util.Locale;
import java.util.ResourceBundle;

public class ResourceBundleclearCache2 {
    public static void main(String[] args) {

        ResourceBundle bundle =
ResourceBundle.getBundle("Hello", Locale.US);

        String key = "Hello";
        System.out.println(bundle.getString(key));

        ResourceBundle.clearCache(null);

    }
}

Output:

Hello Java Program Exception in thread "main" java.lang.NullPointerException at java.base/java.util.Objects.requireNonNull(Objects.java:208) at java.base/java.util.ResourceBundle.clearCache(ResourceBundle.java:2258) at com.example.ListResourceBundle.ResourceBundleclearCache2.main(ResourceBundleclearCache2.java:14)


No comments:

Post a Comment