---
# System prepended metadata

title: final 的用法
tags: [ocpjp]

---

# final 的用法
final 加在class上面, 就是代表不能再被繼承
```java=
A)
final class A1{
	public A1() {};
}

B)
public class Test {
	private static int i;
	private Test() {};
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

C)
final abstract class A5{
	protected static int i;
	void doStuff() {};
	abstract void doIt();
}

D)
class A4{
	protected static final int i;
	private void doStuff() {};
}

E)
abstract class A3{
	private static int i;
	public void doStuff() {}
	public A3() {}
}

```

Which two class definitions fail to compile?
A. Option A
B. Option B
**C. Option C**
**D. Option D**
E. Option E
- [x] **Answer: CD**

:::info
選項C: final 的class 不能被繼承, 所以不能寫abstract
選項D: final i, 不能改變的變數, 所以要給初始值
:::

###### tags: `ocpjp`