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 particlar 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 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()); } } |
Currency Code of India is: INR
Program 2:
// 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()); } } |
Currency Code of USA is: USD
Program 3: For an invalid Currency Code.
// 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); } } } |
java.lang.IllegalArgumentException
Recommended Posts:
- KeyPairGenerator getInstance() method in Java with Examples
- AlgorithmParameterGenerator getInstance() method in Java with Examples
- Calendar getInstance() Method in Java with Examples
- KeyFactory getInstance() method in Java with Examples
- AlgorithmParameters getInstance() method in Java with Examples
- SecureRandom getInstance() method in Java with Examples
- NumberFormat getInstance() method in Java with Examples
- Java Signature getInstance() method with Examples
- DecimalFormatSymbols getInstance() method in Java with Examples
- MessageDigest getInstance() method in Java with Examples
- DecimalFormatSymbols getInstance(Locale) method in Java with Examples
- Currency getSymbol Method in Java with Examples
- Currency toString() Method in Java with Examples
- Currency getDefaultFractionDigits() Method in Java with Examples
- Currency getCurrencyCode() Method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



