Java Learning--inner class (i)

Source: Internet
Author: User

Java Learning--inner class (i) I. Definition and characteristics of internal classes
class Outer{    privite int num = 5;    class Inner{        public void Display(){            System.out.println(num);        }    }    public void method(){        Inner i = new Inner();        i.Display();    }}

This writes a class outer, which has a private variable, a member method (), and an inner class inner. Private variables of the external class outer are accessed in the inner class Inner. The inner class is defined like This:
The class is defined inside other classes, and this class is called an inner class, such as the inner in the previous Example.
The access characteristics of the inner class are:
an Inner class can access members of an external class directly, such as Num in the previous example, including private, and an external class must create an inner class object to access the members of the inner class.
Classification of inner Classes:
* member Inner class
* local inner class

TWO. member Inner Class

Let's start by talking about the difference between a member's inner class and a local inner class.

class Outer{    privite int num = 5;    // 成员位置    class Inner{    }    public void method(){        //局部位置        class Inner2{    }}

Member location: the class defined in the member location, called the member inner class, such as class Inner.
Local location: a class defined in a local location, called a local inner class, such as class Inner2.

How do I access members of members ' inner classes? Here's a straight answer: the external class name . Internal Class Name Object name = External class Object. inner class object

class Outer{    privite int num = 5;    class Inner {        public void Display() {            System.out.println(num);        }    }}class InnerDemo{    public static void main(String[] args) {        // 需要访问Inner类Display方法        Outer.Inner oi = new Outer().new Inner();        oi.show();    }}

For member internal classes, access must be made through classes at the first level.


The modifier for the member inner class is private and Static.
Private is for the security of the Data. when member variables and methods in an inner class do not want to be accessed by an external class or other person, the inner class is declared as Private. The instantiation of the inner class is then placed in the outer class.

class Computer {        private class Cpu {            public void operator() {                System.out.println("更改CPU");            }        }        public void method() {            if(管理者) {                Cpu c = new Cpu();                c.operator();            }        }    }

So when we want to access the operator method in the internal class cpu, because not everyone can change the cpu, the inner class is declared private, and we cannot pass the outer class name Above. internal Class Name Object name = External class Object. internal class object to access, Because this member inner class is private, it does not allow external classes of objects to access this private CPU class. It is then possible to instantiate the inner class by declaring a member method inside the computer to access the private inner class within the computer class.
Static is for easy access to Data. It is important to note that external class data that is accessed by static internal classes must be statically Typed.

class Outer {    private static int num = 10;    public static class Inner {        public void show() {            System.out.println(num);        }           }    public static void show2() {        System.out.println(num2);    }}

This is to access the Show method in the inner class for the external class Name. internal class name Object name = new External Class Name. the inner class name (), which is outer.inner o = new Outer.Inner (), because the inner class is equivalent to a member method, So here the external class Outer can be called directly without Instantiation. Inner (), that is, new Outer.Inner (), when we want to access the Show2 () method, the direct Outer.Inner.show2 (), because it is static.

Three. local Inner class

The definition of the local inner class has been stated Above.

class Outer {    private int num = 10;    public void method() {        class Inner {            public void show() {                System.out.println(num);            }        }        Inner i = new Inner();        i.show();    }}class InnerDemo {    public static void main(String[] args) {        Outer o = new Outer();        o.method();    }}

As can be seen in the above example:
* local inner classes can access members of external classes directly
* in a local location, you can create an inner class object that invokes an inner class method through an object to use the local inner class function
Local inner classes also have an important feature to be aware Of. That is, when local internal classes access local variables, Local variables need to be decorated with the final Keyword. class Outer {private int num = 10;

    public void method() {        final int num2 = 20;        class Inner {            public void show() {                System.out.println(num);                System.out.println(num2);            }        }        Inner i = new Inner();        return i;    }}

The final keyword represents the end Type. Without the final keyword, when the function is finished, the local variable disappears, but the object I generated by the class is on the heap, so the object accesses a non-existent variable. This is a compilation that does not pass. This contradiction is caused by a local inner class that can access local variables but the life cycle of local inner class objects and local variables is Different.

Java Learning--inner class (i)

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.