Java Utility Library

Java Currency - getInstance() Method



The java.util.Currency.getInstance() method returns the Currency instance for the given currency code.

Syntax

public static Currency getInstance(String currencyCode)

Parameters

currencyCode Specify the ISO 4217 code of the currency.

Return Value

Returns the Currency instance for the given currency code.

Exception

  • Throws NullPointerException, if currencyCode is null.
  • Throws IllegalArgumentException, if currencyCode is not a supported ISO 4217 code.

Example:

In the example below, the java.util.Currency.getInstance() method is used to get the currency instance for "GBP".

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a currency instance for "GBP"
    Currency Curr = Currency.getInstance("GBP");

    //getting and printing the ISO 4217 currency code
    String CurrCode = Curr.getCurrencyCode();
    System.out.println("ISO 4217 currency code is: " + CurrCode);
  }
}

The output of the above code will be:

ISO 4217 currency code is: GBP

❮ Java.util - Currency