The Currency class as part of the java.util package is one of the classes of the java api that is not widely used but it provides a significant addition to the language. Let’s tackle the basics of using the class. Let’s discuss first how to instantiate a Currency object
Currency currency = Currency.getInstance(Locale.JAPAN);
Basically the declaration of a Currency object is a little bit different to what we are doing on other classes because it is designed in such a way that there’s never more than one Currency instance for any given currency. Thus the only way is to call the getInstance method of the Currency class.
The getInstance() method is overloaded which takes either a Locale or a String. From our code snippet above we have used the Locale.JAPAN, denoting that we want to use the Japan Currency. In order to print what is the current currency we have set on our Currency object we can use the following:
The above code snippet would print Japanese Yen . This is a convenient method to display the currency which has being used.
If you are interested in learning other useful methods of the Currency class, please go through below list: