Java Internal classes

Source: Internet
Author: User

The Java inner class is divided into four types

---member Inner class, Local inner class, static inner class, anonymous inner class

today, let me tell you something. anonymous inner class

An anonymous inner class is a special local inner class that implements an interface through an anonymous class

Eg:boy is defined as an interface

boy boy=new boy (){};

anonymous inner class features: (we can only use ourselves, others can not tune our methods, implementation of the Class)1, A class is used to inherit other classes or implement interfaces, and there is no need to add additional methods, just prior or overwriting the inherited METHODS. 2, just to get an object instance, you don't need to know its actual type. 3, The class name is meaningless, that is, it does not need to be used.

public class Outer {private static int i = 1;private int j = 10;public static void outer_f1 () {}public void Outer_f2 () {}/ /static inner class can be defined with public,protected,private//static inner class can be static or Non-static member static class inner{static int Inner_i = 100;int Inner_j = 200; static void Inner_f1 () {System.out.println ("outer.i" +i);//static inner class can only access static members of external classes outer_f1 ();//includes static variables and static Methods}void inner_f2 ( {//System.out.println ("outer.i" +j);//static Inner classes cannot access non-static members of external classes//outer_f2 ();//include non-static variables and non-static methods}} public void outer_f3 () {// The external class accesses the static members of the inner class: inner class. static member System.out.println (inner.inner_i); INNER.INNER_F1 ();//external class accesses non-static members of an inner class: instantiates an inner class to Inner Inner = New Inner (); Inner.inner_f2 (); }public static void main (string[] Args) {new Outer (). outer_f3 ();}}note: an anonymous inner class must be behind new, with its implicit implementation of an interface or implementation of a class, without a class name, depending on polymorphism, we use its parent class Name. Because he is a local inner class, all the restrictions on the local inner class are applied to it. Anonymous inner classes are the only class of non-constructed METHODS. Most anonymous inner classes are used for interface callback Calls. Anonymous inner classes are automatically named Out$1.class by the system at compile Time. If the type of an object compile-time is an interface, then its type of operation is the class that implements the Interface. Because the anonymous inner class has no construction method, its use scope is very limited. When multiple objects are needed, local inner classes are used, so the local inner classes are relatively more widely used. The constructor method cannot be defined in an anonymous inner class. If the type of an object compile-time is an interface, then its type of operation is the class that implements the Interface. ________________________________________________________________________________Internal class Summary:1. first, The inner class is treated as a special member of the outer class, so it has a class Member's enclosing level: private, protected, default (friendly), publicit has modifiers for class members: static,final,abstract2. non-static inner class nested inner class, the inner class implies a pointer to an external class this, so it can access all resources of the external class (including, of course, Private)the Outer class accesses the members of the inner class, first to obtain an object of the inner class, and depends on the encapsulation level of the inner class Member. a non-static Inner class cannot contain any static Members.3. Static inner class: static inner class, no longer contains the this pointer of the outer class, and is initialized when the external class is Loaded.static inner classes can contain static or non-static Members.static inner classes can only access external class static Members.External classes Access members of static internal classes, and follow General class Regulations. For static members, The class Name. members are accessible, and for non-static members, you can onlyaccess with the Object. member4. For inner classes within a method or inside a block, only the final variable in the block or method can be Accessed. Class members have two kinds of static, non-static, and the same inner class also has instances of these two non-static inner classes, which must be created in the methods of the outer class or through an instance of the outer class (outerclassinstancename.new Innerclassname (constructorparameter)), and direct access to the information of the external class, the external class object can be outerclassname.this to reference the static instance of the inner class, directly created, There is no reference to the external class Instance. Internal classes whether static or non-static have a reference to an external class non-static inner classes do not allow internal classes in static member methods to allow access only to the final parameter list of the final local variables and methods in the Method. So there's no difference between the inner class and the inner class in the Method. But the inner class in the method cannot be accessed outside the method, and the method cannot have a static inner class---------------------------------------------------------------anonymous inner class if you inherit from an interface, you must implement a method of the specified interface with no parameters |anonymous inner class if inherited from a class, the parameter must be passed as a parameter of the parent Class's constructor |--------------------------------------------------------------- the above is some of my views, if not, please contact me to amend.

Java Internal classes

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.