public static helper class
```java
public class OuterClass {
public static class OuterClassHelper {
static final String FUNCTION_1 = "...";
static final String FUNCTION_2 = "...";
}
}
class TestClass {
public static void main(String[] args) {
System.out.println(OuterClass.OuterClassHelper.FUNCTION_1);
}
}
```
HashMap의 Map.Entry 구현부
```java
static class Node implements Map.Entry {
final int hash;
final K key;
V value;
Node next;
Node(int hash, K key, V value, Node next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
public final K getKey() { return key; }
public final V getValue() { return value; }
public final String toString() { return key + "=" + value; }
public final int hashCode() {
return Objects.hashCode(key) ^ Objects.hashCode(value);
}
public final V setValue(V newValue) {
V oldValue = value;
value = newValue;
return oldValue;
}
public final boolean equals(Object o) {
if (o == this)
return true;
if (o instanceof Map.Entry) {
Map.Entry,?> e = (Map.Entry,?>)o;
if (Objects.equals(key, e.getKey()) &&
Objects.equals(value, e.getValue()))
return true;
}
return false;
}
}
```
멤버 클래스에서 바깥 인스턴스에 접근할 일이 없다면 무조건 static 으로 선언하라!
비정적 멤버 클래스
바깥 클래스의 인스턴스 없이 생성 불가능
비정적 멤버 클래스 인스턴스 내에 바깥 인스턴스와의 관계가 만들어짐
바깥 인스턴스로의 숨은 참조가 생김
이 참조는 메모리 공간을 차지하여 생성 시간이 더 소요됨
GC가 바깥 클래스의 인스턴스를 수거하지 못하여 메모리 누수가 생길수도 있음
보이지 않는 참조라 원인 파악도 어려움
주로 어댑터를 정의할 때 자주 쓰임
어떤 클래스의 인스턴스를 감싸 마치 다른 클래스의 인스턴스처럼 보이게 하는 것
Map, List, Set 처럼 다른 컬렉션 인터페이스를 내부 클래스로 구현하지만 바깥 클래스 멤버에 접근해야할 때 사용
HashMap의 EntrySet 구현부
```java
final class EntrySet extends AbstractSet> {
public final int size() { return size; }
public final void clear() { HashMap.this.clear(); }
public final Iterator> iterator() {
return new EntryIterator();
}
public final boolean contains(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry,?> e = (Map.Entry,?>) o;
Object key = e.getKey();
Node candidate = getNode(hash(key), key);
return candidate != null && candidate.equals(e);
}
public final boolean remove(Object o) {
if (o instanceof Map.Entry) {
Map.Entry,?> e = (Map.Entry,?>) o;
Object key = e.getKey();
Object value = e.getValue();
return removeNode(hash(key), key, value, true, true) != null;
}
return false;
}
public final Spliterator> spliterator() {
return new EntrySpliterator<>(HashMap.this, 0, -1, 0, 0);
}
public final void forEach(Consumer super Map.Entry> action) {
Node[] tab;
if (action == null)
throw new NullPointerException();
if (size > 0 && (tab = table) != null) {
int mc = modCount;
for (Node e : tab) {
for (; e != null; e = e.next)
action.accept(e);
}
if (modCount != mc)
throw new ConcurrentModificationException();
}
}
}
```
중첩 클래스 (nested class)
새로운 클래스가 필요한데 이 클래스를 하나의 클래스에서만 사용한다면 중첩 클래스 사용
중첩 클래스 종류
정적 멤버 클래스 (static class)
Map 구현체의 Entry
public static helper class
```java public class OuterClass { public static class OuterClassHelper { static final String FUNCTION_1 = "..."; static final String FUNCTION_2 = "..."; } } class TestClass { public static void main(String[] args) { System.out.println(OuterClass.OuterClassHelper.FUNCTION_1); } } ```HashMap의 Map.Entry 구현부
```java static class Node멤버 클래스에서 바깥 인스턴스에 접근할 일이 없다면 무조건 static 으로 선언하라!
비정적 멤버 클래스
HashMap의 EntrySet 구현부
```java final class EntrySet extends AbstractSet익명 클래스 (Anonymous class)
Comparable
,Runnable
...람다를 쓰자
지역 클래스 (Local class)
잘 안씀