Thursday, July 23, 2026
HomeLanguagesJavaCurrency getInstance() Method in Java with Examples

Currency getInstance() Method in Java with Examples

The getInstance() Method of Currency class in Java is used to retrieve the instance of this currency for a given currency code.
Syntax: 
 

CURRENCY.getInstance(String currency_code)

Parameters: This method accepts one parameter currency_code which is the currency of a particular currency.
Return Value: This method returns the instance of the currency for a currency code.
Exceptions: The method throws Runtime Error if an invalid code is called.
Below program illustrates the working of getInstance() method:
Program 1:
 

Java




// Java Code to illustrate getInstance() method
 
import java.util.*;
 
public class Currency_Demo {
    public static void main(String[] args)
    {
 
        // Creating a currency with the code
        Currency curr_ency
            = Currency.getInstance("INR");
 
        // Getting the currency code
        System.out.println("Currency Code of India is: "
                           + curr_ency.toString());
    }
}


Output: 

Currency Code of India is: INR

 

Program 2:
 

Java




// Java Code to illustrate toString() method
 
import java.util.*;
 
public class Currency_Demo {
    public static void main(String[] args)
    {
 
        // Creating a currency with the code
        Currency curr_ency
            = Currency.getInstance("USD");
 
        // Getting the currency code
        System.out.println("Currency Code of USA is: "
                           + curr_ency.toString());
    }
}


Output: 

Currency Code of USA is: USD

 

Program 3: For an invalid Currency Code.
 

Java




// Java Code to illustrate getInstance() method
 
import java.util.*;
 
public class Currency_Demo {
    public static void main(String[] args)
    {
        try {
 
            // Creating a currency with the code
            Currency curr_ency
                = Currency.getInstance("USDA");
 
            // Getting the currency code
            String currency_code
                = curr_ency.toString();
            System.out.println("Invalid Currency Code: "
                               + currency_code);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}


Output: 

java.lang.IllegalArgumentException

 

RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32520 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6902 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12115 POSTS0 COMMENTS
Shaida Kate Naidoo
7023 POSTS0 COMMENTS
Ted Musemwa
7265 POSTS0 COMMENTS
Thapelo Manthata
6980 POSTS0 COMMENTS
Umr Jansen
6971 POSTS0 COMMENTS