سؤال

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