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