---
# System prepended metadata

title: enum的用法
tags: [ocpjp]

---

# enum的用法
```java=
enum USCurrency{
	PENNY(1),
	NICKLE(5),
	DIME(10),
	QUARTER(25);
	private int value;
	public USCurrency(int value){
		this.value = value;
	}
	public int getValue() {
		return value;
	}
}
public class Test {
	public static void main(String[] args) {
		USCurrency usCoin = new USCurrency.DIME;
		System.out.println(usCoin.getValue());
	}
}
```

## Which two modifications enable the given code to compile?
A. Nest the USCurrency enumeration declaration within the Coin class.
**B. Make the USCurrency enumeration constructor private.**
**C. Remove the new keyword from the instantion of usCoin.**
D. Make the getter method of value as a static method.
E. Add the final keyword in the declaration of value.
- [x] **Answer: BC**

:::info
enum建構子的可見度只能使用default或是private。使用enum預先列舉好的常數不需要使用new運算子。因此這題答案是選項B和選項C。
:::

###### tags: `ocpjp`