Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top