Java Currency getInstance() Method

There are two types of getIsntance methods.

Java Currency getInstance(Loacale locale) Method

The getInstance(Locale locale) is the method of Java Currency class which is used to get an instance of currency for the given country. Because countries change their currencies, the result may vary over time.

Syntax

Following is the declaration of getInstance() method:

Parameter

DataTypeParameterDescription
NALocaleFor whose country a currency code is needed.

Returns

This method will return the instance of Currency for the given locale of country, or it will return null.

Exceptions

NullPointerException - If locale or currencyCode is null.

IllegalArgumentException ? If the country of locale is not a supported ISO 3166 country code.

Compatibility Version

Java 1.4 and above

Example 1

Output:

Currency code of given locale: EUR

Example 2

Output:

Currency code UK =  GBP
Currency code US =  USD

Example 3

Output:

Currency code is: EUR

Example 4

Output:

---------Indian Currency----------
Currency display name : Indian Rupee
Currency Code : INR
Currency Numeric Code : 356

Java Currency getInstance(String currencyCode) Method

The getInstance() is the method of Java Currency class which is used to get an instance of currency for the given currency code.

Syntax

Following is the declaration of getInstance(String currencyCode) method:

Parameter

DataTypeParameterDescription
StringcurrencyCodeThe ISO 4217 code of the currency.

Returns

This method returns the instance of currency for the given currency code.

Exceptions

NullPointerException - If currencyCode is null.

IllegalArgumentException - If currencyCode is not a supported ISO 4216 code.

Compatibility Version

Java 1.4 and above

Example 1

Output:

Indian Rupee

Example 2

Output:

Currency code of US: USD
 Currency name of US: US Dollar

Example 3

Output:

Enter the Currency name: INR
Currency is: Indian Rupee

Example 4

Output:

---------Indian Currency----------
Currency display name: Indian Rupee
Currency Code: INR
Currency Numeric Code: 356