Immutability:
String are immutable.
Wrappers are immutable.
Integer iWrap = new Integer(42);
There is no setter method for a wrapper object.
You can't change the value of String/Wrapper unless you refer it to another object.
Assertions:
To run with assertions:
java -ea applicaiton
Static nested classes are just like a static member.
class is either public or default.
default: keep everything in the package.
protected: default and allows subclasses to inherit the protected thing outside the package.
Enumerations
public enum Members {JERRY, BOBBY, PHIL};
public Members member;
// == and equals() are the same.
Constant-specific class body:
public class EnumTest {
enum Names {
JERRY("lead guitar") {
@Override
public String sings() {
return "heihei";
}
},
BOBO("bass");
private String instrument;
Names(String instrument) {
this.instrument = instrument;
}
public String getInstrument() {
return instrument;
}
public String sings() {
return "hahaha";
}
}
public static void main(String[] args) {
for (Names n : Names.values()) {
System.out.println(n + " " + n.getInstrument() + " " + n.sings());
}
}
}
Elements are static and must be declared at the beginning.
Enumeration can have constructors and methods since it is a class.