Java Inner Class (inner class) detailed

Source: Internet
Author: User
Tags event listener modifiers

Advantage ⒈ the inner class object can access the implementation of the object that created it, including private data, ⒉ internal classes are not seen by other classes in the same package, and are well encapsulated, ⒊ can easily write event drivers using internal classes, and ⒋ anonymous inner classes can easily define runtime callbacks; 5. Internal classes can be easily defined Class members inner classes, method inner classes, anonymous inner classes, static inner classes, inner classes

1. The member inner class is the most common inner class, which is defined as being inside another class and can unconditionally access all member properties and member methods of the outer class (including private and static members)

Attention:

When a member inner class has a member variable or method with the same name as an external class, a hidden phenomenon occurs, that is, by default, members of the members inner class are accessed. If you want to access a member of the same name as an external class, you need to access it in the following form:

External class. This. member variable external class. This member method

In an external class, if you want to access members of a member's inner class, you must first create an object of the member's inner class, and then access it by referring to the object

A member's inner class exists when it is attached to an external class, if it is to create an object of a member's inner class, provided that an object of an outer class must exist. The general way to create a member's inner class object is as follows:

public class Outerclass {    private double radius = 0;        Private Innerclass getinnerclassinstance () {        return new innerclass ();    }         Class Innerclass {     //internal class public        void Drawsahpe () {            System.out.println (RADIUS);  Private member of external class        }    } public        static void Main (string[] args)  {        //First way:    Outerclass Outter = new Outerclass ();    Outerclass.innerclass inner = outter.new innerclass ();  The INNER.DRAWSAHPE () must be created through the Outter object    ;                 The second way:    outerclass.innerclass inner1 = Outter.getinnerclassinstance ();    INNER1.DRAWSAHPE ();    }}

Internal classes can have private access, protected access, public access, and package access. For example, if the member inner class inner with private decoration, it can only be accessed inside the external class, if it is modified with public, it can be accessed anywhere, if decorated with protected, it can only be accessed under the same package or inherit the external class;

2, the method inner class (local inner Class) is defined in a method or a scope inside the class, and the member inside the class is the difference between the local inner class access is limited to within the method or within the scope.

Inner class, Anonymous inner class in non-static method

1), an inner class of a non-static method can access an instance variable of an external class, a static variable

2), the inner class of a non-static method can access the final variable of the method

Inner class, Anonymous inner class in static method

1), the inner class of the static method can access the static variables of the outer class, but cannot access the external variables of the instance

2), the inner class of the static method can access the final variable of the method, but cannot access the non-final variable of the method

Note

A local inner class is like a local variable inside a method, and it cannot have public, protected, private, or static modifiers.

3. Anonymous inner class

An anonymous inner class is generally used to write the event listener code. Similarly, anonymous inner classes cannot have access modifiers and static modifiers;

Anonymous inner classes are the only classes that do not have constructors. Because it does not have a constructor, the use of anonymous internal classes is very limited, and most anonymous inner classes are used for interface callbacks. Anonymous inner classes are automatically named Outter$1.class by the system at compile time. In general, anonymous inner classes are used to inherit other classes or implement interfaces, and there is no need to add additional methods, just implementations of inherited methods or overrides.

public class Button {public    void click () {        ///Anonymous inner class, implemented by ActionListener interface        new ActionListener () {            public void OnAction () {                System.out.println ("Click action ...");}        . OnAction ();    }    An anonymous inner class must inherit or implement an existing interface public    interface actionlistener{public        void OnAction ();    }    public static void Main (string[] args) {        button button=new button ();        Button.Click ();    }}

4. Static internal class

A static inner class is also a class that is defined in another class, except that there is a keyword static in front of the class.

Static inner classes do not need to depend on the outer class, which is somewhat similar to the static member property of the class, and it cannot use the non-static member variable or method of the outer class, because the object of the static inner class can be created without an object of the outer class. If you allow access to non-static members of an external class, there is a contradiction because the non-static members of the outer class must be attached to the specific object.

public class Outerclass {private Double radius = 0;    Static class Innerclass {     //Statically internal class public     void Drawsahpe () {             //system.out.println (RADIUS);  Cannot access non-static member of Outer class         }    } public        static void Main (string[] args)  {    Outerclass.innerclass inner = new Outerclass.innerclass ();    }}
Inner classes can access external class elements directly, but external classes cannot directly access the elements of an inner class
public class Outerclass {    private String outername;    public class innerclass{        private int innername;        Innerclass () {            //inner class can access elements of external class            Outername= "outer class";        }        public void display () {            System.out.println (outername);        }    }    public static void Main (string[] args) {        Outerclass outerclass = new Outerclass ();        Outerclass.innerclass innerclass = Outerclass.new innerclass ();        Innerclass.display ();
Outerclass.innername; Error, external class cannot directly access elements of inner class }}

Inner classes can access external class properties directly, although the external class properties are decorated with private. When an external class is created, the inner class automatically captures a reference to an external class, so the inner class accesses the outer class element, which is actually accessed through the external class reference that he holds.

Relationships between internal classes and external classes

The inner class is a compile-time concept, and the compiled external class and its inner classes generate two separate class files, and the inner class is a relatively independent entity, and the external class is not a relationship of is a

How do external classes access inner class elements?
public class Outerclass {public    void display () {        //external class accesses the inner class element and requires indirect access through the internal class reference        Innerclass innerclass=new Innerclass ();        Innerclass.innerdisplay ();    }    public class innerclass{public        void Innerdisplay () {            System.out.println ("I am Inner Class");        }    }    public static void Main (string[] args) {        outerclass outerclass=new outerclass ();        Outerclass.display ();    }}

About inheritance issues for members ' internal classes. In general, inner classes are rarely used as inheritance. But when used to inherit, pay attention to two points:

1) The member inner class must be referenced in Outter.inner.

2) The constructor must have a reference to the Outer class object and call Super () through this reference.

Class Withinner {    class inner{             }}class Inheritinner extends Withinner.inner {          //Inheritinner () is not compiled, Be sure to add the formal parameter    inheritinner (Withinner wi) {        wi.super ();//must have this call    } public      static void Main (string[] args) { C7/>withinner WI = new Withinner ();        Inheritinner obj = new Inheritinner (WI);}    }

Resources:

The idea of Java programming

http://www.cnblogs.com/dolphin0520/

    

Java Inner Class (inner class) detailed

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.