Internal classes in Java

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.