External class call internal class

Source: Internet
Author: User

The syntax format for defining internal classes (including static and non-static) variables outside of external classes is as follows:
Outerclass. innerclass varname
From the above syntax, we can see that when an internal class is used outside the external class, the complete Class Name of the internal class should

Outerclass. innerclass. Of course, if the external class has a package name, you should also add the package name prefix.
Because non-static internal class objects must be stored in external class objects, before creating non-static internal class objects, you must first

Create an object for its external class. The syntax for creating non-static internal class instances in the external class is as follows:
Outinstance. New innerconstructor ().
From the preceding syntax format, we can see that external class instances and new must be used to create non-static internal class instances outside the external class.

Call the constructor of a non-static internal class. BelowProgramHow to create non-static internal class objects outside the external class, and

Assign it to a variable of the non-static internal class type.

class out {
// defines an internal class without the access control operator, that is, other classes in the same package can access this internal class
class in {
Public in (string MSG) {
system. out. println (MSG);
}< BR >}< br> public class createinnerinstance {
Public static void main (string [] ARGs) {
out. in in = new out (). new in ("test information");
}< BR >}< br>

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.