Java Internal class Learning notes

Source: Internet
Author: User

This is my note on learning the Java inner class

1. Why use internal classes?
The most appealing reason for using inner classes is that each inner class can inherit one (interface) implementation independently, so whether or not the perimeter class has inherited an implementation of (an interface),
has no effect on inner classes
1.1. The greatest advantage of using an inner class is that it solves multiple inheritance problems very well, and the use of internal classes can also give us the following features:
(1), the inner class can use multiple instances, each with its own state information, and the information of other peripheral objects is unique to each other.
(2), in a single perimeter class, you can have multiple inner classes implement the same interface in different ways, or inherit the same class.
(3), the time to create an inner class object does not depend on the creation of the perimeter class object.
(4), the inner class does not have the confusing "is-a" relationship, he is an independent entity.
(5), the inner class provides a better encapsulation, except for the perimeter class, which cannot be accessed by other classes.
2. Internal class Classification:
(i). member Inner class:

1234567891011121314151617181920 public class Outer{        private int age = 99;        String name = "Coco";        public class Inner{            String name = "Jayden";            public void show(){                System.out.println(Outer.this.name);                System.out.println(name);                System.out.println(age);            }        }        public Inner getInnerClass(){            return new Inner();        }        public static void main(String[] args){            Outer o = new Outer();            Inner in = o.new Inner();            in.show();        }    }

The

1.Inner class is defined inside the Outer class and is equivalent to the position of a member variable of the Outer class, and the Inner class can use any access control,
as defined in
2.Inner classes such as public, protected, private, and so on. The show () method can directly access data in the Outer class without being affected by the access control,
such as direct access to private properties in the Outer class, age
3. Once you have defined the member inner class, you must use an external class object to create the inner class object instead of going directly to the new An inner class object,
that is: An inner class object name = External class object. New inner Class ();
4. After compiling the above program, you will find that there are two. class files: outer.class,outer$inner.class{}
5. There can be no static variables and methods in the member's inner class, and constants may be defined:
(1). Because a non-static inner class is dependent on an instance of an external class, static variables and methods are independent of the object, only related to the class, and
in short: There is no external class at all when loading a static domain, where a static field or method cannot be defined in a non-static inner class, and the compilation does not pass;
The scope of a non-static inner class is the instance level
(2). Constants are determined in the compiler, placed in the so-called constant pool
★★ friendly tip:
1. An external class cannot directly use the members and methods of an inner class, you can first create an object of an inner class, and then access its member variables and methods through an object of the inner class;
2. If the external class and the inner class have the same member variable or method, the inner class accesses its own member variable or method by default, and if you want to access the member variable of the external class,
You can use the This keyword, such as: Outer.this.name
(ii). Static INNER class: Yes Static decorated inner class,
1. Static inner classes cannot directly access non-static members of an external class, but can access   through the new external class () member.
2. If the static member of the external class is the same as the member name of the inner class, you can pass the class name. Static member Accesses a static member of an external class;
If the static member of the external class is not the same as the member name of the inner class, the static member of the external class can be called directly through the member name
3. When you create an object of a static inner class, you do not need an object of the outer class, you can directly create an inner class object name

123456789101112131415 public class Outer{            private int age = 99;            static String name = "Coco";            public static class Inner{                String name = "Jayden";                public void show(){                    System.out.println(Outer.name);                    System.out.println(name);                                  }            }            public static void main(String[] args){                Inner i = new Inner();                i.show();            }        }

(iii). Method Inner class: Access is limited to within a method or within that scope
(1). The local inner class is like a local variable inside a method, and cannot have public, protected, private, and static modifiers.
(2). Only local variables of the final type defined in the method can be accessed because:
When the method is called to run, the local variable is extinct. But the inner class object may still exist,
It will not die until it is referenced. At this point, there will be a situation, that is, the internal class to access a non-existent local variables;
==> using the final modifier will not only keep the object's references from changing, but the compiler will also continue to maintain the object's life cycle in the callback method.
A local inner class is not a parameter that is called directly by a method, but an inner class that backs up the passed-in parameters to its own internal structure.
Its own internal method call is actually its own property rather than the parameters of the external class method;
Prevents tampering with the data, resulting in inconsistent values from the inner class

1234567891011121314151617181920212223242526    /*        使用的形参为何要为 final???         在内部类中的属性和外部方法的参数两者从外表上看是同一个东西,但实际上却不是,所以他们两者是可以任意变化的,         也就是说在内部类中我对属性的改变并不会影响到外部的形参,而然这从程序员的角度来看这是不可行的,         毕竟站在程序的角度来看这两个根本就是同一个,如果内部类该变了,而外部方法的形参却没有改变这是难以理解         和不可接受的,所以为了保持参数的一致性,就规定使用 final 来避免形参的不改变         */        public class Outer{            public void Show(){                final int a = 25;                int b = 13;                class Inner{                    int c = 2;                    public void print(){                        System.out.println("访问外部类:" + a);                        System.out.println("访问内部类:" + c);                    }                }                Inner i = new Inner();                i.print();            }            public static void main(String[] args){                Outer o = new Outer();                o.show();            }        }    

(iv). Anonymous INNER class:
(1). An anonymous inner class is a reference that uses new directly to generate an object;
(2). For the use of anonymous inner classes it is a flaw, that it can only be used once, when an anonymous inner class is created, it immediately creates an instance of the class.
The definition of the class disappears immediately, so the anonymous inner class is not reusable;
(3). When using anonymous inner classes, we must either inherit a class or implement an interface, but both cannot be combined, and can only inherit one class or implement an interface;
(4). Anonymous inner classes are not defined by constructors, and no static member variables or static methods exist in an anonymous inner class;
(5). Anonymous inner classes cannot have any static member variables and static methods, and an anonymous inner class cannot be abstract, it must implement all the abstract methods of an inherited class or an implemented interface
(6). Anonymous inner class initialization: Use to construct blocks of code! The effect of creating a constructor for an anonymous inner class can be achieved by building blocks of code

123456789101112131415161718   public class OuterClass {            public InnerClass getInnerClass(final intnum,String str2){                return new InnerClass(){                    int number = num + 3;                    public int getNumber(){                        return number;                    }                };        /* 注意:分号不能省 */            }            public static void main(String[] args) {                OuterClass out = new OuterClass();                InnerClass inner = out.getInnerClass(2, "chenssy");                System.out.println(inner.getNumber());            }        }        interface InnerClass {            int getNumber();        }         

 

Java Internal class Learning notes

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.