Home >Java >JavaInterview questions >Java internal classes and reflection interview questions
#1. What is the difference between static nested class and inner class?
Static nested class: Static Nested Class is an inner class declared as static (static), which can be instantiated without relying on external class instances.
Inner class: It needs to be instantiated after the external class is instantiated before it can be instantiated. Its syntax looks quite weird. (Recommended study: java interview questions)
2. Where in the following code will compilation errors occur?
class Outer { class Inner { } public static void foo() { new Inner(); } public void bar() { new Inner(); } public static void main(String[] args) { new Inner(); } }
Note: The creation of non-static inner class objects in Java depends on its outer class objects. In the above interview questions, the foo and main methods are both static methods. There is no this in the static method, that is It says that there is no so-called outer class object, so inner class objects cannot be created. If you want to create an inner class object in a static method, you can do this:
new Outer().new Inner();
Reflection in Java
Tell me about your understanding of reflection in Java
Reflection in Java is first able to obtain the bytecode of the reflection class in Java. There are three ways to obtain the bytecode:
Class.forName(className).
Class name.class.
this.getClass().
Then the methods, variables, constructors, etc. in the bytecode are mapped into corresponding Method, Field, Constructor and other classes. These classes provide a wealth of methods that can be used by us.
The above is the detailed content of Java internal classes and reflection interview questions. For more information, please follow other related articles on the PHP Chinese website!