- μ μ λ©€λ² ν΄λμ€ (static member class)
- λΉμ μ λ©€λ² ν΄λμ€
- μ΅λͺ ν΄λμ€
- μ§μ ν΄λμ€
- ν΄λμ€ λ΄λΆμ static ν΄λμ€
λ°κΉ₯ ν΄λμ€μ private λ©€λ²μλ λ°λ‘ μ κ·Ό κ°λ₯. μ΄μΈμλ μΌλ° ν΄λμ€μ κ°λ€.
- private μ μ λ©€λ² ν΄λμ€ : λ°κΉ₯ ν΄λμ€μ κ΅¬μ± μμλ₯Ό λνλΌ λ
public class Person {
private String firstName;
private String lastName;
private static class Computer { // private, public
private String name;
private int price;
public Computer(String name, int price) {
this.name = name;
this.price = price;
}
public int getPrice() {
return price;
}
}
}
- public μ μ λ©€λ² ν΄λμ€ : λ°κΉ₯ ν΄λμ€μ ν¨κ» μ°μΌ λλ§ μ μ©ν public λμ°λ―Έ ν΄λμ€
public class Calculator {
public enum Operation1 {
PLUS(Integer::sum),
MINUS((x, y) -> x - y);
private BiFunction<Integer, Integer, Integer> calculate;
Operation1(BiFunction<Integer, Integer, Integer> calculate) {
this.calculate = calculate;
}
public BiFunction<Integer, Integer, Integer> getFunction() {
return calculate;
}
}
public int Sum(int x, int y) {
return Operation1.PLUS.getFunction().apply(x, y);
}
}
public static void main(String[] args) {
Calculator c = new Calculator();
System.out.println(c.Sum(1, 2)); // 3
System.out.println(Calculator.Operation1.PLUS); // PLUS
}
- λΉμ μ λ©€λ² ν΄λμ€μ μΈμ€ν΄μ€λ λ°κΉ₯ ν΄λμ€μ μΈμ€ν΄μ€μ μ묡μ μΌλ‘ μ°κ²°
- λ°λΌμ, λΉμ μ λ©€λ² ν΄λμ€μ μΈμ€ν΄μ€ λ©μλμμ μ κ·νλ this λ₯Ό μ¬μ©νμ¬ λ°κΉ₯ μΈμ€ν΄μ€μ μ°Έμ‘°λ₯Ό κ°μ Έμ¬ μ μμ.
μ κ·νλ this == ν΄λμ€λͺ .this
- λΉ μ μ λ©€λ² ν΄λμ€λ μΈμ€ν΄μ€λ₯Ό κ°μΈμ λ§μΉ λ€λ₯Έ ν΄λμ€μ²λΌ 보μ΄κ² νλ λ·°μΈ μ΄λν°μμ μ£Όλ‘ μ°μΈλ€.
public class TreeMap<K,V> extends AbstractMap<K,V> implements NavigableMap<K,V>, Cloneable, java.io.Serializable {
...
// View class support
class Values extends AbstractCollection<V> {
public Iterator<V> iterator() {
return new ValueIterator(getFirstEntry());
}
public int size() {
return TreeMap.this.size();
}
public boolean contains(Object o) {
return TreeMap.this.containsValue(o);
}
public boolean remove(Object o) {
for (Entry<K,V> e = getFirstEntry(); e != null; e = successor(e)) {
if (valEquals(e.getValue(), o)) {
deleteEntry(e);
return true;
}
}
return false;
}
public void clear() {
TreeMap.this.clear();
}
public Spliterator<V> spliterator() {
return new ValueSpliterator<>(TreeMap.this, null, null, 0, -1, 0);
}
}
class EntrySet extends AbstractSet<Map.Entry<K,V>> {
public Iterator<Map.Entry<K,V>> iterator() {
return new EntryIterator(getFirstEntry());
}
public boolean contains(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<?,?> entry = (Map.Entry<?,?>) o;
Object value = entry.getValue();
Entry<K,V> p = getEntry(entry.getKey());
return p != null && valEquals(p.getValue(), value);
}
public boolean remove(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<?,?> entry = (Map.Entry<?,?>) o;
Object value = entry.getValue();
Entry<K,V> p = getEntry(entry.getKey());
if (p != null && valEquals(p.getValue(), value)) {
deleteEntry(p);
return true;
}
return false;
}
}
...
}
- TreeMap.this, getFirstEntry() λ λ°κΉ₯ ν΄λμ€μ μΈμ€ν΄μ€κ° μμ΄μΌ μΈ μ μλ€.
- λ°λΌμ λΉμ μ λ©€λ² ν΄λμ€μ μΈμ€ν΄μ€μμ κ΄κ³μ λ³΄κ° μ μ₯λμ΄ λ©λͺ¨λ¦¬ 곡κ°μ μ°¨μ§νκ³ , μμ± μκ°λ λ κ±Έλ¦°λ€.
- λν λ°κΉ₯ ν΄λμ€μ μΈμ€ν΄μ€λ₯Ό GC κ° νμνμ§ λͺ»νλ€.
public Set<Map.Entry<K,V>> entrySet() {
EntrySet es = entrySet;
return (es != null) ? es : (entrySet = new EntrySet());
}
μμ μ½λμμ λΉμ μ λ©€λ² ν΄λμ€ EntrySetμ κ°μ²΄λ₯Ό μμ±νλ€. μ묡μ μΌλ‘ λ°κΉ₯ ν΄λμ€μ λ©€λ² ν΄λμ€κ°μ μ°κ²°λλ κ΄κ³κ° λΉμ μ λ©€λ² ν΄λμ€μ μΈμ€ν΄μ€ μμ λ§λ€μ΄μ§κ³ ,
Map κ°μ²΄κ° μ¬μ©νλ κ³³μ΄ μλλΌλ μ΄ κ΄κ³λλ¬Έμ GC κ° μΌμ΄λμ§ λͺ»νλ€.