Java Internal Class Learning summary

Source: Internet
Author: User

Catalogue

    • Directory
    • Overview
    • Non-static inner class
      • Instantiating an inner class from a non-static method of an external class
      • Instantiating an inner class from a static method in an external class
      • The This reference for the inner class
    • Static Inner class
      • Instantiating a static inner class from a non-static method of an external class
      • Instantiating a static inner class from an external class static method
    • Anonymous inner class
    • Method Inner Class

Overview

Learning Python recently and discovering that Python supports multiple inheritance reminds me of the mechanism that Java implements through internal classes. This article is not about how to implement multiple inheritance through an inner class, but rather summarizes the types and usage of the inner classes.

The Java inner classes are divided into:

    • Non-static inner class
    • Static Inner class
    • Local inner class
    • Anonymous inner class

Internal classes are used extensively in the Android source code, first introduce the common denominator of these four inner classes:

    1. The inner class is still a separate class, and after compilation The inner class is compiled into a separate. class file, but preceded by the class name and the $ symbol of the outer class.
    2. Internal classes cannot be accessed in an ordinary way. An inner class is a member of an external class, because an inner class can freely access members of an external class, including private members.
    3. If the inner class is declared static, you cannot arbitrarily access the member variables of the outer class, at which point the inner class can only access static member variables of the outer class.

Next, describe these inner classes separately.

non-static inner class

When a class is a non-static member of another class, the class is a non-static inner class.
The sample code for creating a non-static inner class is as follows:

class OutClass {    class InnerClass {}}

When we used Javac to compile, we found that two. class files were generated: Outclass.class and Outclass$innerclass.class. As shown in the following:

instantiating an inner class from a non-static method of an external class

It is easy to access the inner class in an external class, create the inner class object directly, and then invoke the method within the class through the object instance. The sample code is as follows:

 Public  class outclass {    Private Static intA =0; Public void Makeinner() {Innerclass Inclass =NewInnerclass ();    Inclass.seeouter (); } Public Static void Main(string[] args) {Outclass Oclass =NewOutclass ();    Oclass.makeinner (); } class Innerclass { Public void Seeouter() {System.out.println (a);        a++; }    }}

The results of the operation are as follows:

0
instantiating an inner class from a static method in an external class

Accessing an inner class in an external class is relatively straightforward and can be used to create an inner class object directly, but if you want to use an inner class outside the outer class, you cannot directly new the inner class name, but you need to do the following:

new OutClass().new InnerClass();

That is, calling a non-static inner class externally requires an instance of the external class and then instantiating the inner class through the outer classes object. The sample code is as follows:

 Public  class outclass {    Private Static intA =0; Public void Makeinner() {Innerclass Inclass =NewInnerclass ();    Inclass.seeouter (); } Public Static void Main(string[] args) {Outclass Oclass =NewOutclass ();        Oclass.makeinner (); Outclass.innerclass Innerclass =NewOutclass ().NewInnerclass ();    Innerclass.seeouter (); } class Innerclass { Public void Seeouter() {System.out.println (a);        a++; }    }}

Operation Result:

01
the This reference for the inner class

A normal class can use this to refer to the current object, as is the inner class. But what if the inner class wants to refer to the current object of the outer class? You can use the following methods:

外部类名.this

The sample code is as follows:

 Public  class outclass {    Private Static intA =0; Public void Makeinner() {Innerclass Inclass =NewInnerclass ();    Inclass.seeouter (); } Public Static void Main(string[] args) {Outclass Oclass =NewOutclass ();        Oclass.makeinner (); Outclass.innerclass Innerclass =NewOutclass ().NewInnerclass ();    Innerclass.seeouter (); } class Innerclass { Public void Seeouter() {System.out.println ( This); System.out.println (outclass. This); }    }}
static inner class

The non-static inner class is described above, and next we learn that the God horse is a static inner class.

A static inner class is a role that plays a static member in an external class, similar to creating a static inner class and creating a non-static inner class, except that there is a static modifier in front of the class.

Note that it is not possible for an external class to be decorated with the static modifier.

The sample code is as follows:

class OutClass {    static class InnerClass {    }}

Compiled with the Javac command, you can see that there are two. class files, as shown in:

instantiating a static inner class from a non-static method of an external class

Accessing a static inner class from an external class is the same as accessing a non-static inner class in the outer classes. However, it is important to note that static internal classes can only access static members of external classes and cannot access non-static members.

The sample code is as follows:

 Public  class outclass {    Private Static intA =0;Private intb =1; Public void Makeinner() {Innerclass Inclass =NewInnerclass ();    Inclass.seeouter (); } Public Static void Main(string[] args) {Outclass Oclass =NewOutclass ();    Oclass.makeinner (); }StaticClass Innerclass { Public void Seeouter() {System.out.println ( This); System.out.println (a);//System.out.println (b);}    }}

The results of the implementation are as follows:

OutClass$InnerClass@79a3400
instantiating a static inner class from an external class static method

Attention:
Because a static inner class is a static member of an external class, static members are bound to the class and not to the object instantiated by the class. Therefore, it is not necessary to instantiate the inner class in the static method of the outer class.

The sample code is as follows:

 Public  class outclass {    Private Static intA =0;Private intb =1; Public void Makeinner() {Innerclass Inclass =NewInnerclass ();    Inclass.seeouter (); } Public Static void Main(string[] args) {Outclass Oclass =NewOutclass ();        Oclass.makeinner (); Outclass.innerclass Inclass =NewOutclass.innerclass ();    Inclass.seeouter (); }StaticClass Innerclass { Public void Seeouter() {System.out.println ( This); System.out.println (a);//System.out.println (b);}    }}
Anonymous Inner class

Anonymous internal classes are rampant in Android app development, and many of the listener objects are implemented through anonymous internal classes.

Anonymous inner class You can tell from a name this is an inner class that represents no class name, and is often used to simplify the code.

Believe that the students who write Java have used threads, that thread when we can pass a Runnable object, you can also pass an anonymous inner class. The sample code is as follows:

 Public  class outclass {     Public void Testanonymousclass() {Thread T =NewThread (NewRunnable () {@Override             Public void Run() { for(inti =0; I <Ten; i + +) {System.out.println (i);Try{Thread.Sleep ( -); }Catch(Interruptedexception e)                    {E.printstacktrace ();        }                }            }        });        T.start (); System.out.println ("Another thread is running ..."); } Public Static void Main(string[] args) {Outclass Oclass =NewOutclass ();    Oclass.testanonymousclass (); }}

The results of the implementation are as follows:

isrunning...0123456789
method Inner Class

The inner class of a method, by definition, is to place the class in the method, and its scope is also the scope of the method.

However, I don't think the inner class of the method has any eggs, so this is not the case, it is so capricious.

Java Internal Class Learning summary

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.