java.util.Currency getCurrencyCode()

Description

As we have discussed, the Currency class is specially designed so that there’s never more than one Currency instance for any given currency. Because of this design, the Currency class has no no public constructor. Instead, you can obtain a Currency instance using the getInstance methods. And there are two overloaded method for the getInstance().

getInstance(Locale locale) – this method returns the Currency instance for the country of the given locale. The language and variant components of the locale are ignored. The result may vary over time, as countries change their currencies. For example, for the original member countries of the European Monetary Union, the method returns the old national currencies until December 31, 2001, and the Euro from January 1, 2002, local time of the respective countries.
The method returns null for territories that don’t have a currency, such as Antarctica.

getInstance(String currencyCode) – Returns the Currency instance for the given currency code.

Method Syntax

The getInstance() method is overloaded which means that we have more than one getInstance method but with different method signature. Below are the overloaded method:

1.

public static Currency getInstance(String currencyCode)

2.

public static Currency getInstance(Locale locale)

Method Returns

There are two possible return values of the getInstance method, depending on the method argument and here are the following:

getInstance(String currencyCode) – this will return a Currency instance for the given currency code.

getInstance(Locale locale) – this method will return the Currency instance for the country of the given locale, or null.

Compatibility

Java 1.4

Java Currency getInstance() Example

Since the getInstance() method has two overloaded methods, we would be 2 java example as well.

Below is a simple java example on the usage of getInstance(Locale locale) method of Currency class.

package com.javatutorialhq.java.examples;


import static java.lang.System.*;

import java.util.Currency;
import java.util.Locale;


/*
 * This example source code demonstrates the use of 
 * getInstance(Locale locale) method of Currency class.
 */

public class CurrencyGetInstanceLocaleExample {

	public static void main(String[] args) {	
		
		// create an instance of Currency class using a specified locale
		Currency currency = Currency.getInstance(Locale.FRANCE);
		// prints the currency of France
		out.println(currency.getDisplayName());		
	}
}

Basically on the above example, we just created a new Currency object with France as locale. Then we use the getDisplayName() to get the currency of France.

Below is the sample output when you run the above example which shows how to use the getInstance(Locale locale) method.

Currency getInstance(Locale locale) Sample Output

Below is a simple java example on the usage of getInstance(String currencyCode) method of Currency class.

package com.javatutorialhq.java.examples;


import static java.lang.System.*;

import java.util.Currency;
import java.util.Locale;


/*
 * This example source code demonstrates the use of 
 * getInstance(String currencyCode) method of Currency class.
 */

public class CurrencyGetInstanceCurrencyCodeExample {

	public static void main(String[] args) {	
		
		// create an instance of Currency class using a specified locale
		Currency currency = Currency.getInstance("JPY");
		// prints the currency of Japan
		out.println(currency.getDisplayName());		
	}
}

Basically on the above example, we just created a new Currency object with Currency code of JPY which corresponds to the currency of Japan. Then we use the getDisplayName to get the currency of Japan.

Below is the sample output when you run the above example which shows how to use the getInstance(String currencyCode) method.

Currency getInstance(String currencyCode) Sample Output