Java internal class learning Summary

Source: Internet
Author: User

Java internal class learning Summary
Directory

Directory Overview non-static internal classes instantiate internal classes from external classes non-static methods instantiate internal classes from static methods of external classes this reference static internal classes from external classes static Method instantiation static internal class from external class static method static class anonymous internal class method internal class

Overview

Recently I learned python and found that python supports multi-inheritance, which reminds me that Java is implemented through internal classes. This article does not explain how to implement multi-inheritance through internal classes, but summarizes the types and usage of internal classes.

Java internal classes include:

Non-static internal class local internal class anonymous internal class

Internal classes are widely used in Android source code. First, we will introduce the similarities of these four internal classes:

The internal class is still an independent class. After compilation, the internal class will be compiled into an independent. class file, but the class name and $ symbol of the external class are prefixed. Internal classes cannot be accessed in normal mode. An internal class is a member of an external class, because the internal class can freely access members of an external class, including private members. When the internal class is declared static, it cannot access the member variables of the external class at will. In this case, the internal class can only access the static member variables of the external class.

Next, we will introduce these internal classes respectively.

Non-static internal class

When a class acts as a non-static member of another class, this class is a non-static internal class.
The example code for creating a non-static internal class is as follows:

class OutClass {    class InnerClass {}}

When we use javac for compiling, we find that two. class files are generated: OutClass. class and OutClass $ InnerClass. class. As shown in:

Instantiate an internal class from a non-static method of an external class

It is easy to access internal classes in external classes. You can directly create internal class objects and call methods in the class through the object instance. The sample code is as follows:

public class OutClass {    private static int a = 0;    public void makeInner() {        InnerClass inClass = new InnerClass();        inClass.seeOuter();    }    public static void main(String[] args) {        OutClass oClass = new OutClass();        oClass.makeInner();    }    class InnerClass {        public void seeOuter() {            System.out.println(a);            a++;        }    }}

The running result is as follows:

0
Instantiate an internal class from a static method of an external class

Accessing an internal class in an external class is relatively simple. You can directly create an internal class object. However, if you want to use an internal class outside the external class, you cannot directly use the new internal class name. Instead, you must use the following method:

OutClass.InnerClass innerClass = new OutClass().new InnerClass();

That is to say, in external calls to non-static internal classes, you must first instantiate the external class, and then instantiate the internal class through the external class object. The sample code is as follows:

public class OutClass {    private static int a = 0;    public void makeInner() {        InnerClass inClass = new InnerClass();        inClass.seeOuter();    }    public static void main(String[] args) {        OutClass oClass = new OutClass();        oClass.makeInner();        OutClass.InnerClass innerClass = new OutClass().new InnerClass();        innerClass.seeOuter();    }    class InnerClass {        public void seeOuter() {            System.out.println(a);            a++;        }    }}

Running result:

01
This reference of internal classes

A common class can use this to reference the current object. The same applies to internal classes. But what if the internal class wants to reference the current object of the external class? You can use the following method:

External class name. this

The sample code is as follows:

public class OutClass {    private static int a = 0;    public void makeInner() {        InnerClass inClass = new InnerClass();        inClass.seeOuter();    }    public static void main(String[] args) {        OutClass oClass = new OutClass();        oClass.makeInner();        OutClass.InnerClass innerClass = new OutClass().new InnerClass();        innerClass.seeOuter();    }    class InnerClass {        public void seeOuter() {            System.out.println(this);            System.out.println(OutClass.this);        }    }}
Static internal class

The above describes non-static internal classes. Next we will learn that Shenma is a static internal class.

A static internal class assumes the role of a static member in an external class. Creating a static internal class is similar to creating a non-static internal class, but a static modifier is added before the class.

Note that external classes cannot be modified using static modifiers.

The sample code is as follows:

class OutClass {    static class InnerClass {    }}

Compile it with the javac command. We can see that there are two. class files, as shown in:

Instantiate a static internal class from a non-static method of an external class

Accessing static internal classes from external classes is the same as accessing non-static internal classes from external classes. Note that the static internal class can only access static members of the external class and cannot access non-static members.

The sample code is as follows:

public class OutClass {    private static int a = 0;    private int b = 1;    public void makeInner() {        InnerClass inClass = new InnerClass();        inClass.seeOuter();    }    public static void main(String[] args) {        OutClass oClass = new OutClass();        oClass.makeInner();    }    static class InnerClass {        public void seeOuter() {            System.out.println(this);            System.out.println(a);            // System.out.println(b);        }    }}

The execution result is as follows:

OutClass$InnerClass@79a3400
Instantiate a static internal class from an external static Class Method

Note:
Because static internal classes are static members of external classes, static members are bound to classes, rather than objects instantiated by classes. Therefore, to instantiate an internal class in a static method of an external class, you do not need to instantiate an external class first.

The sample code is as follows:

public class OutClass {    private static int a = 0;    private int b = 1;    public void makeInner() {        InnerClass inClass = new InnerClass();        inClass.seeOuter();    }    public static void main(String[] args) {        OutClass oClass = new OutClass();        oClass.makeInner();        OutClass.InnerClass inClass = new OutClass.InnerClass();        inClass.seeOuter();    }    static class InnerClass {        public void seeOuter() {            System.out.println(this);            System.out.println(a);            // System.out.println(b);        }    }}
Anonymous internal class

Anonymous internal classes are widely used in Android Application development. Many implementations of various listener objects use anonymous internal classes.

The anonymous internal class can be known from the name that it represents an internal class without a class name, which is usually used to simplify the code.

I believe that all the students who write Java have used threads. In this case, we can upload a Runnable object or an anonymous internal class. The sample code is as follows:

public class OutClass {    public void testAnonymousClass() {        Thread t = new Thread(new Runnable() {            @Override            public void run() {                for (int i = 0; i < 10; i ++) {                    System.out.println(i);                    try {                        Thread.sleep(500);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        });        t.start();        System.out.println("another thread is running...");    }    public static void main(String[] args) {        OutClass oClass = new OutClass();        oClass.testAnonymousClass();    }}

The execution result is as follows:

another thread is running...0123456789
Internal method class

The internal class of the method, as its name implies, is to put the class in the method, and its scope is also the scope of the method.

However, I don't think there is any way to use the internal class of the method, so I won't talk about it here, it's so capricious.

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.