How to execut main method, if it is present in static inner class?
Main method is present in static inner class and we need to execute that main method.
class A {
static class B {
public static void main(String[] args) {
System.out.println("Done");
}
}
}
Try something like this:
java A$B
Update according to comments:
In linux shell you should escape $. So the command became:
java 'A$B'
Its just like simple class. Run command java A$B
When inner class is compiled, it is prepended with outer class name
In this case you two class files. i.e . A.class
and A$B.class
java
command takes the classname as the argument and not the filenamejava A$B
will do the work OuterClass$1
, OuterClass$1
and so on.So if you modify your example as follows, now including anonymous and method local inner classes
import java.io.Serializable;
public class A {
static class B {
public static void main(String[] args) {
System.out.println("Done");
Serializable obj = new Serializable() {
};
Serializable obj1 = new Serializable() {
};
class MethodLocalClass {
}
}
}
}
Then the class files you will get are A.class
, A$B.class
, A$B$1.class
, A$B$2.class
for the anonymous classes and A$B$1MethodLocalClass.class
.
Hope this example helps a bit :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With