문제

Trying to check if class K is a static class of A

class A {
    private static class K {
        static final int MODE1 = 1;
        static final int MODE2 = 2;
    }
}

Class<?> c = A.class;
for( Class<?> item: c.getDeclaredClasses() ) {
    if( Modifier.isStatic(item.getModifiers()) ) {
        if( "K".equals(item.getSimpleName()) ) {
            // found it!
        }
    }
}

Is this the only way? To iterate through all declared classes? For methods we have getDeclaredMethod(), for fields we have getDeclaredField(), but TTBOMK there is no getDeclaredClass() or something similar.

도움이 되었습니까?

해결책

Do you mean like this?

Class a = A.class;
Class k = Class.forName(a.getName()+"$K");

I don't imagine there is often done, so you might not have a more "friendly" method.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top