Java: Anonymous class, anonymous internal class, and java Anonymous class

Source: Internet
Author: User

Java: Anonymous class, anonymous internal class, and java Anonymous class
Content:

 

  • Internal class
  • Anonymous class

 

Release date:

Internal class:
  • Define another class in one class. The class defined in this way is called an internal class. [Classes containing internal classes can be called external classes of internal classes]
  • If you want to use another class through one class, you can define it as an internal class. [For example, Apple mobile phones and the gold edition of Apple mobile phones are customized]
  • The member variables of the external class of the internal class are still valid in the internal class. Methods in the internal class can also call methods in the external class. [Whether static or non-static, internal classes can directly call attributes in external classes ,]
  • Class variables and class methods cannot be declared in the class bodies of internal classes.
  • Internal classes can be used by external classes to create internal class objects in functions in external classes. If the internal class permissions are non-private and non-static, you can access other external programs by creating external class objects. If the internal class is static and non-private, static members can call the class name directly, non-static members are used by creating external class objects.
class Outer{    int a=5;    static int b=6;    void show() {        System.out.println("hello world");    }    class Inner{        void use() {            System.out.println(a);//5            System.out.println(b);//6            show();//hello world                    }    }    void create() {        new Inner().use();    }}public class Demo {    public static void main(String[] args) {        new Outer().create();        Outer.Inner oi=new Outer().new Inner();        oi.use();    }}

 

 

 

Supplement:
  • The bytecode files of internal classes are different. Will be changed to external class name $ internal class name
  • Defines the internal class on a local location.
    • All members of the external class can be accessed.
    • Local internal classes cannot access local variables (originally, their lifecycles are different, the space of the internal class object does not disappear, and the object lifecycle is long). If you need to access these classes, add final to modify the variables, add final to a constant. (Jdk1.8 automatically modifies final)
  • If the internal class is defined as static, the lifecycle is no different from the normal static class.

 

Anonymous class:
  • An anonymous class is a class without a name. Its name is provided by the Java compiler. It is generally in the form of external class name + $ + Anonymous class order, there is no name, that is, it cannot be referenced elsewhere. It cannot be instantiated. It is only used once. Of course, there cannot be a constructor.
  • An anonymous class is constructed into a class by using the constructor of the parent class and its own class body.
    • In the above format, the "parent class" is a subclass class or interface that needs to be inherited or implemented externally.
  • An anonymous class can inherit the methods of the parent class or override the methods of the parent class.
  • Anonymous classes can access member variables and methods of external classes. The class bodies of anonymous classes cannot be declared as static member variables or static methods.
  • Since an anonymous class is a new result, you can assign a value to a parent class object. Therefore, it can be divided into two types of anonymity: Member Anonymity and partial anonymity (as function parameters)

 

 

An example of implementing an interface method:

 

class Outer{    void show() {        System.out.println("run in Outer");    }}public class Demo {    public static void main(String args[]) {        Outer ot=new Outer(){        void show() {            System.out.println("run in Inner");        }    };    ot.show();//run in Inner    }}

 

 

 

 

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.