Inner classes in Java (Vomiting blood summary)

Source: Internet
Author: User

1. Internal class classification

1) Ordinary inner class

Class is related to an instance, and can be seen as an instance variable. The class name of the inner class is determined by the outer class. Inner class.

Ordinary inner classes cannot declare static-related variables or methods. An inner class can directly access all members of an external class, including private members, implicitly or explicitly (external class. This). While the outer class can be new to the inner class, the instance-related can be directly new,static related (class-related) need to use the instance reference to refer to. New. You can add private and protected in front of class classes to indicate that only their external classes or their subclasses have access.

Final class Demo {    private int m = 4;    Private Demo () {        System.out.println ("demo initial");    }    Private class Inner {        private int i = +;        Inner () {            System.out.println ("Inner initial");        }        private void F () {            System.out.println ("F method" + "I:" + this.i + "M:" + demo.this.m);        }    }    public static void Main (string[] args) {        Demo demo = new Demo ();        System.out.println ("Dispatch Inner class");        Demo.inner inn = Demo.new Inner ();        INN.F ();    }} /* Out:demo Initialdispatch Inner classinner initialf method i:23 m:4*///~

2) Local inner class

Inner classes in methods and scopes are called local inner classes and are accessible only in their lexical scope. Constructors can be overloaded.

3) Anonymous Inner class

Anonymous inner Class A variable that references an external class must be final because the anonymous inner class does not have a complete class structure and cannot access the external class members by referencing a reference to the outer class, but instead uses the value copy to access the value of the outer member of the copy. The value must therefore be final decorated.

The anonymous inner class does not have a named constructor, and new Wrapper (x) {} can pass the value to the constructor of the base class.

4) Nested classes

If you do not need to have a connection between an inner class object and its external class object, you can declare an inner class as static, which is referred to as an inner class or a nested class. It is characterized by the creation of an instance of a nested class that does not require an instance of its enclosing class, an instance of a non-static perimeter class that cannot be accessed from an instance of a nested class, and a nested class that is not initialized with an external class load. Classes inside the interface are automatically public static, and nested classes can be placed in the interface so that they can be shared by different implementations of the interface.

Final class Demo {    private int m = 4;    private static int n = 6;    Private Demo () {        System.out.println ("demo initial");    }    Static class Inner {        private int i =;        Inner () {            System.out.println ("Inner initial");        }        private void F () {            System.out.println ("F method" + "I:" + this.i + "N:" + DEMO.N);        }    }    public static void Main (string[] args) {//        Demo.inner inn = new Inner ();//Inner initial//        inn.f ();//F method I:23 N:6        New Demo ();//Demo initial    }}/* Out:demo initial*///~

2. Role

The inner class is a separate entity that can be used to implement closures, communicate with external classes, and internal classes and interfaces to make multiple inheritance more complete.

3. Use

Inner classes in Java (Vomiting blood summary)

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.