8
class test {
    public static void main(String[] args) {
         new test();
    }
    void method() {
         class inside {
              int a;
              void methodinside() {}
         }
    }
}

I was declaring class using reflection like:

Class c = Class.forName("test");
Class[] cls = c.getDeclaredClasses();
for(Class cl : cls)
     System.out.println(cl.getName());

but, my program cannot find class inside.

6
  • 1
    Interesting, since we can do the reverse with getEnclosingClass. ideone.com/nA3Zua Commented Apr 16, 2015 at 3:09
  • how to access in main, while class inside in another method or another class ??? Commented Apr 16, 2015 at 3:19
  • If you want to access the class from main then you have to declare it somewhere where main can see it. The point of these local classes is to not be visible from outside the declaring method. Commented Apr 16, 2015 at 3:34
  • any idea or other way beside reflection ??? Commented Apr 16, 2015 at 3:37
  • This could be an X-Y-problem. What are you really trying to do? Are you developing some kind of IDE tooling? Commented Apr 16, 2015 at 4:49

4 Answers 4

2

I don't think there is a method to find local classes (classes defined inside a method).

You could try to enumerate all classes in the package and then use getEnclosingClass (the opposite of what you are looking for) to filter them.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't believe there's a way to enumerate all classes in a package either.
try you see on class files. there are created file test$1inside.class
2

This is a way to get Inner class in a method.

public class Main {
    public static void main(String[] args) throws ClassNotFoundException {
        Class<?> clazz = Class.forName("Outer$1InnerMethodClass");
        System.out.println(clazz.getName());

        clazz = Class.forName("Outer$2InnerMethodClass");
        System.out.println(clazz.getName());

        clazz = Class.forName("Outer$2InnerMethodClass$1InnerMethodInnetMethod");
        System.out.println(clazz.getName());
    }

}

class Outer{
    void outerMethod1() {
        class InnerMethodClass {
             int a;
             void innerMethod() {}
        }
   }

    void outerMethod2() {
        class InnerMethodClass {
             int a;
             void innerMethod() {
                 class InnerMethodInnetMethod{

                 }
             }
        }
   }
}

1 Comment

This can't be safely generalized though. The exact name of a local class is not mandated by the specification, only its format.
2

In Java, there aren't general methods for enumerating classes by design. This is because classes could be loaded from anywhere, or dynamically created. Storing class files in a JAR or directory is just one of any possible class loaders.

So, while it might be more reasonable to assume that enclosed classes are a known, enumerable set, it doesn't make sense to provide a method that works only in that special case.

If you assume the common case of a URLClassLoader, you can treat the underlying URLs as file systems and enumerate their contents. This might be appropriate if you are creating a tool to do something with existing third-party code, and it has even been adopted for many Java EE standards (mistakenly, I feel).

If you are creating a plug-in mechanism, providing metadata in a well-known location is more efficient, reliable, and works with any valid class loader.

Comments

1

The getDeclaredClasses() method returns (from the javadoc)-

an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object. This includes public, protected, default (package) access, and private classes and interfaces declared by the class, but excludes inherited classes and interfaces.

So it seems it doesn't return any inner class you have declared. That's why you get nothing in the array of Class cls.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.