Dark Horse programmer —————— > Using internal classes

Source: Internet
Author: User

-------Android Training, Java training, look forward to communicating with you! ----------

The primary role of defining a class is to define variables, create instances, and inherit as parent classes. The main role of defining inner classes is the same, but using internal classes to define variables and create instances has some minor differences with external classes. The usage of the inner class is discussed in three scenarios below.

1: Use inner class in outer class inner class

When using inner classes inside an external class, it is not very different from ordinary classes. You can define variables directly from the Inner class class name and create an instance by calling the inner class constructor with new.

The only difference is that you do not use non-static inner classes in static members of external classes, including static methods and static initialization blocks, because static members cannot access non-static members.

The subclass of the inner class defined inside the outer class is not much different from the usual definition subclass.

2: Use non-static inner classes outside the outer class

If you want to access inner classes (both static and non-static) outside of the class, the inner class cannot use private access control permissions, and the inner class of the privat adornment can only be used inside the outer class, and for other access-control-modifier-Internal classes, Can be used within the access control for mortal access.

(1) Omit the internal class of the access control, which can only be accessed by other classes that are in the same package as the outer class.

(2) An inner class that is decorated with protrcted can be accessed by subclasses of other classes and external classes of mortals in the same package as the outer class.

(3) An inner class that is decorated with public can be accessed from anywhere.

The syntax for defining inner classes (including both static and non-static) variables outside the outer class is as follows:

Outerclass.innerclass VarName

As you can see, when using an inner class outside the outer class, the complete class name of the inner class should be outerclass.innerclass if the external class has a package name, you should also increase the package name prefix.

Because objects of non-static inner classes must be parasitic in an object of an outer class, the Outer class object must be created before a non-static inner class object is created. The syntax for creating an instance of a non-static inner class outside the outer class is as follows:

Outerinstance. New Innerconstructor ()

As you can see, an instance of creating a non-static inner class outside of the class must use an external class instance and new to invoke the constructor of the non-static inner class. The following procedure demonstrates how to create an object of a non-static inner class outside the outer class and assign it to a variable of a non-static inner class type.

1 class out2 {3     //defines an inner class that does not use the access control character4     //That is, only the other classes in the same package can access the inner class5     classinch6     {7          PublicIn (String msg)8         {9 System.out.println (msg);Ten         } One     } A } -  -  Public classcreateinnerinstance the { -      Public Static voidMain (string[] args) -     { -Out.in in =NewOut ().NewIn ("Test information"); +          -         /** + * The above code can be changed to the following three lines of code A * Define internal class variables using Outerclass.innerclass form at * out.in in; - * Create an instance of an external class, and a non-static inner class instance will be parasitic in that instance - * out off = new Out (); - * Invoking an inner class constructor with an external class instance and new to create a non-static inner class instance - * in = Out.new in ("Test Information"); -          */ in     } -}

an object of a non-static inner class was created in the program above. As you can see, constructors for non-static inner classes must be invoked using an external class object.
If you need to create a subclass of a non-static inner class outside the outer class, pay particular attention to the rule above: the constructor of a non-static inner class must be called through its outer class object. When a subclass is created, the subclass constructor always calls the constructor of the parent class, so when you create a subclass of a non-static inner class, you must ensure that the subclass constructor can invoke the construction of a non-static inner class, and an outer class object must exist when the constructor of a non-static inner class is called. The following program defines a subclass that inherits the non-static inner class in class of the Out class.

class extends out.in{    public  Subclass (out)            {//  Explicitly calls in the constructor out through the incoming out object        .  Super("Hello");    }    }

Constructors for non-static inner class in classes must be called with an external class object, and super in the code represents the constructor that calls the in class, while out represents the outer class object.

If you are creating a subclass object, you must first create an out object. This is reasonable, because subclass is a subclass of the non-static inner class in class, and a reference to an out object must be in the nonstatic inner class in object, and its subclass subclass object should also hold a reference to the Out object. The out object that is passed to the constructor when the subclass object is created is the object that the Out object reference in the subclass object refers to.

The non-static inner class in object and the subclass object must hold a reference to an Out object, except that when you create two objects, you pass in the Out object in a different way: when you create a non-static inner class in object, you must call the New keyword through the Out object; When you subclass an object of a class, you must call the in constructor using the out object as the caller.

A subclass of a non-static inner class is not necessarily an inner class, it can be an external class. A subclass instance of a non-static inner class, however, needs to keep a reference to the object of the outer class whose parent class resides. That is, if an object of an inner class subclass exists, there must be an outer class object corresponding to it.

Using static internal classes outside of external classes

Because static inner classes are related to external class classes, it is not necessary to create an external class object when creating a static inner class object. The syntax for creating a static inner class instance outside the outer class is as follows:

New Outerclass.innerconstructor ()

The following procedure demonstrates how to create an instance of a static inner class outside the outer class.

1 classStaticout2 {3     //defines a static inner class that does not use the access control character4     //That is, other classes in the same package can access the inner class5     Static classstaticin6     {7          Publicstaticin ()8         {9System.out.println ("Constructor for static inner class");Ten         } One     } A } -  -  Public classcreatestaticinnerinstance the { -  -      Public Static voidMain (string[] args) -     { +Staticout.staticin in =Newstaticout.staticin (); -          +         /** A * The above code can be the following two lines of code at * Define internal class variables using Outerclass.innerclass form - * staticout.staticin in; - * Create a static inner class instance by invoking the inner class constructor with new - * in = new staticout.staticin (); -          */ -     } in  -}

As you can see, both static inner classes and non-static inner classes declare variables in exactly the same syntax. Distinguishing knowledge when you create an inner class object, the static inner class only needs to use the outer class to invoke the constructor, and the non-static inner class must use an external class object to invoke the constructor.

Because it is not necessary to use an external class object when invoking a constructor of a static inner class, it is simpler to create a subclass of a static inner class.

class extends staticout.staticin{}

When a static inner class is defined, its outer class is much like a package space.

In contrast, using static internal analogies to use non-static inner classes is much simpler, as long as the outer class is treated as a static inner class of the package space. Therefore, when you need to use internal classes, you should prioritize static inner classes.

Dark Horse programmer —————— > Using internal classes

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.