SCJP 考題中的陷阱
原著 Ajith Kallambella
(1) Two public classes in the same file. (illegal)
一個 source file 只能有一個 public class
(2) Main method calling a non-static method. (illegal)
static 不能夠存取非 static 成員。(程式進入點的 main(),是 static method)
(3) Methods with the same name as the constructor(s). (這種題常有)
方法名稱可以跟 Constructor 和 Class 名稱相同
constructor 不能有回傳值、也不能夠被繼承
(4) Thread initiation with classes that do not have a run() method. (常考之題)
繼承自 Thread 的類別,必須要 override Thread class 的 run()(預設是無
內容)
(5) Local inner classes trying to access non-final vars. (illegal)
Local inner class 絕對不能夠存取非 final 的 local var
(6) Case statements with values out of permissible range. (byte, int, short, char)
注意「四大金剛」的值的範圍
範圍:
byte :
char :
short :
int :
(7) Math class being an option for immutable classes !! (totally wrong!)
Math Class 無法改變,也無法覆寫。它的方法是 static、final 的 method
(8) instanceOf is not same as instanceof. (注意 O 大小寫之分)
instanceof 是用來判斷某物件屬於何種類別物件的運運算元。
(9) Private constructors. (legal)
Constructor 可以用 public、private、protected、( default)修飾
(10) An assignment statement which looks like a comparison.
比如說 if(a=true) [應該是 if(a = = true)]
评论0