Today, a person in the group raised an internal class question. Although I have seen it before, it cannot solve his problem. Now I want to organize it:
Public class Outer {
Inner in = new inner ();
Public static void main (string [] ARGs ){
New outer ();
System. Out. println ("this is main .");}
Class inner extends outer {
}
}
This is the program he gave. After I try it, the following error will be reported during compilation: exception in thread "Main" Java. Lang. stackoverflowerror
I checked the following information:
What should I know about internal classes?
1. The internal class is an independent individual and has nothing to do with the external class.
2. The internal class can operate all member methods and member variables of the external class, including private modified.
3. generate an internal class bytecode file in the format of external class name $ internal class name.
4. When an external class, internal class, or internal class method contains the same field or method, when the method calls that field or method, the field in the method is called first, to call fields in the class, you can use: This. field name. To call fields in an external class, use outer. this. field name.
5. If you need to declare an internal class, you can define a method for returning an internal class object to the external class. You can also create a new () External class object in the new class, and then use "outer. inner () "is used to declare external class variables. Note that internal classes cannot be declared directly with new, or internal class objects cannot be directly defined in external classes.
6. No matter how deep the internal class is nested, you can access the objects (variables and methods) of the external class ).
The solution in the group is: A class cannot have non-static class objects, directly or indirectly subclass objects;
In fact, you can refer to the fifth point. You cannot directly define internal class objects in the external class!