내부 클래스의 예

AbstractList 클래스 에는 Itr 내부 클래스가 있습니다 . 컬렉션의 요소를 하나씩 가져올 수 있는 Iterator 인터페이스 의 구현입니다 .

private class Itr implements Iterator<E> {
	int cursor = 0;
	int lastRet = -1;
	int expectedModCount = modCount;

	public boolean hasNext() {
    		return cursor != size();
	}

	public E next() {
    	checkForComodification();
    	try {
        	int i = cursor;
        	E next = get(i);
        	lastRet = i;
        	cursor = i + 1;
        	return next;
    	} catch (IndexOutOfBoundsException e) {
        	checkForComodification();
        	throw new NoSuchElementException(e);
    	}
	}

	public void remove() {
    	if (lastRet < 0)
        	throw new IllegalStateException();
    	checkForComodification();

    	try {
        	AbstractList.this.remove(lastRet);
        	if (lastRet < cursor)
            	cursor--;
        	lastRet = -1;
        	expectedModCount = modCount;
    	} catch (IndexOutOfBoundsException e) {
   	     throw new ConcurrentModificationException();
    	}
	}

	final void checkForComodification() {
    	if (modCount != expectedModCount)
        	throw new ConcurrentModificationException();
	}
}

반복자 메서드 에서 사용됩니다 .

public Iterator<E> iterator() {
	return new Itr();
}

이것은 AbstractList 의 후손이 기성 반복자를 얻는 방법입니다. 반복자를 사용자 지정해야 하는 경우 Iterator 또는 Itr 을 상속하는 자체 클래스를 구현한 다음 반복자 메서드를 재정의할 수 있습니다. 예를 들어 이것은 ArrayList 클래스가 하는 일입니다.

Itr 클래스는 정적이 아닙니다 . 결과적으로 Itr 개체는 AbstractList 인스턴스 에 대한 참조를 가지며 해당 메서드( size , get , remove )에 액세스할 수 있습니다.

정적 중첩 클래스의 예

Integer 클래스 에는 IntegerCache 중첩 클래스가 있습니다 .

private static class IntegerCache {
	static final int low = -128;
	static final int high;
	static final Integer[] cache;
	static Integer[] archivedCache;

	static {
    	int h = 127;
    	String integerCacheHighPropValue =
        	VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
    	if (integerCacheHighPropValue != null) {
        	try {
            	h = Math.max(parseInt(integerCacheHighPropValue), 127);
            	h = Math.min(h, Integer.MAX_VALUE - (-low) -1);
        	} catch( NumberFormatException nfe) {
        	}
    	}
    	high = h;

    	VM.initializeFromArchive(IntegerCache.class);
    	int size = (high - low) + 1;

    	if (archivedCache == null || size > archivedCache.length) {
        	Integer[] c = new Integer[size];
        	int j = low;
        	for(int i = 0; i < c.length; i++) {
            	c[i] = new Integer(j++);
        	}
        	archivedCache = c;
    	}
    	cache = archivedCache;
    	assert IntegerCache.high >= 127;
}

	private IntegerCache() {}
}

IntegerCache는 캐시를 생성하고 캐시 범위와 캐시된 값 자체를 저장하는 기능을 캡슐화합니다. 따라서 캐시와 관련된 모든 것은 별도의 클래스에 보관됩니다. 이렇게 하면 코드를 더 쉽게 읽고 수정할 수 있습니다. 클래스를 사용하는 코드:

public static Integer valueOf(int i) {
	if (i >= IntegerCache.low && i <= IntegerCache.high)
    		return IntegerCache.cache[i + (-IntegerCache.low)];
	return new Integer(i);
}

IntegerCache 클래스는 Integer 클래스 의 비정적 필드 및 메서드에 액세스하지 않습니다 . 또한 정적 valueOf 메서드 에서만 액세스됩니다 . 즉, 개별 인스턴스가 아니라 Integer 클래스 자체 에 바인딩됩니다 . 즉, IntegerCache 는 정적입니다.

익명 내부 클래스의 예

익명 클래스의 예로 InputStream 과 정적 nullInputStream 메서드를 살펴보겠습니다.

public static InputStream nullInputStream() {
    return new InputStream() {
    	private volatile boolean closed;

    	private void ensureOpen() throws IOException {
        	if (closed) {
            		throw new IOException("Stream closed");
        	}
    	}

    	@Override
    	public int available () throws IOException {
        	ensureOpen();
        	return 0;
    	}

    	@Override
    	public int read() throws IOException {
        	ensureOpen();
        	return -1;
    	}

    	@Override
    	public int read(byte[] b, int off, int len) throws IOException {
        	Objects.checkFromIndexSize(off, len, b.length);
        	if (len == 0) {
            		return 0;
        	}
        	ensureOpen();
        	return -1;
    	}

    	@Override
    	public byte[] readAllBytes() throws IOException {
        	ensureOpen();
        	return new byte[0];
    	}

    	@Override
    	public int readNBytes(byte[] b, int off, int len)throws IOException {
        	Objects.checkFromIndexSize(off, len, b.length);
        	ensureOpen();
        	return 0;
    	}

    	@Override
   	 public byte[] readNBytes(int len) throws IOException {
        	if (len < 0) {
            		throw new IllegalArgumentException("len < 0");
        	}
        	ensureOpen();
        	return new byte[0];
    	}

    	@Override
    	public long skip(long n) throws IOException {
        	ensureOpen();
        	return 0L;
    	}

    	@Override
    	public void skipNBytes(long n) throws IOException {
        	ensureOpen();
        	if (n > 0) {
            		throw new EOFException();
        	}
    	}

    	@Override
    	public long transferTo(OutputStream out) throws IOException {
        	Objects.requireNonNull(out);
        	ensureOpen();
        	return 0L;
    	}

    	@Override
    	public void close() throws IOException {
        	closed = true;
    	}
    };
}

이 메서드는 익명 클래스에 의해 구현된 빈 InputStream 을 반환합니다. 이 클래스는 후손이 없어야 하므로 익명으로 만들었습니다.

Java Stream API가 추가되면서 익명 클래스가 유비쿼터스화되었습니다. 모든 람다 식은 일부 기능 인터페이스를 구현하는 익명 클래스입니다. 몇 가지 예를 고려하십시오.

AbstractStringBuilder 클래스에는 유명한 StringBuilderStringBuffer 클래스의 상위 클래스 포함되어 있습니다 .

@Override
public IntStream chars() {
	return StreamSupport.intStream(
        	() -> {
            	byte[] val = this.value;
            	int count = this.count;
            	byte coder = this.coder;
            	return coder == LATIN1
                   	? new StringLatin1.CharsSpliterator(val, 0, count, 0)
                   	: new StringUTF16.CharsSpliterator(val, 0, count, 0);
        	},
        	Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED,
        	false);
}

Files 클래스 에는 Closeable 을 Runnable 로 변환하는 기능이 있습니다 .

private static Runnable asUncheckedRunnable(Closeable c) {
	return () -> {
    	try {
        	c.close();
    	} catch (IOException e) {
        	throw new UncheckedIOException(e);
    	}
	};
}

Class 클래스 에는 메서드의 문자열 표현을 가져오는 메서드가 있습니다.

private String methodToString(String name, Class<?>[] argTypes) {
	return getName() + '.' + name +
        	((argTypes == null || argTypes.length == 0) ?
        	"()" :
        	Arrays.stream(argTypes)
        	        .map(c -> c == null ? "null" : c.getName())
                	.collect(Collectors.joining(",", "(", ")")));
}