Java Experience---Inner class, anonymous class, Static inner class

Source: Internet
Author: User




1. A Java program is a collection of classes.                                                                 A class in a program has two kinds of arrangement structures.












For example: classenclosingclass{

.....






Class anestedclass{

......

}






             }






2. In some special cases, a class is also placed within the internal definition of another class, the class defined within the other class is called an inner class (sometimes called a nested Class), and a class containing an inner class is called an outer class (sometimes called a host class).






the function of the inner class:






The inner class provides a better encapsulation that hides the inner class within the outer class and does not allow other classes in the same package to be accessed.






3.






the internal class is privileged in the external class: An inner class can access all members of the outer classes without restrictions (including private members). However, an external class cannot access the implementation details of an inner class, such as the properties of an inner class.






after compiling, the inner class can also produce a separate. class file with the name: external class name $ internal class name. class






Unlike ordinary external classes, the inner class is a member of the outer class, and the inner class object cannot exist alone, and it must rely on an external class object






• using anonymous inner classes is handy when writing event-handling-driven programs.






• only internal classes can be private-security, and only the methods of their external classes can create internal class objects.






for other classes in the same package, the inner class can be hidden (the inner class can be decorated with private)






3. If you only need to create an object for an inner class, you don't even have to specify a name for the class, which is called an anonymous inner class.






it is appropriate to use an anonymous inner class if some of the following conditions are true:






U only use one instance of a class






the U class is used immediately after the definition






the U class is very small (recommended below 4 lines of code)






• naming a class doesn't make your code easier to understand






4. The inner class can also be called a local inner class in a method or statement block. Local inner classes are the same as the basic attributes of a member class, such as an instance property of a local inner class that must belong to an instance of its external class, and can refer to an instance of an external class by using the external class. class, but it can only use the final constant in the method or statement block.






For example:






class a{






Public Void Show () {






final int a = ten;






class b{






B () {






System.out.println (a);






}






}






}






}






an inner class can be defined as an abstract class or interface, but it must be inherited or implemented by other inner classes. A non-static inner class cannot declare a static member, only a static inner class can declare a static member.






5. An internal class without a name is called an anonymous inner class.






General class:






Class name implementsactionlistener{






Public voidactionperformed (ActionEvent e) {...}






}  






new class name ();






anonymous inner class:






New ActionListener () {

Public voidactionperformed (ActionEvent e) {...}

};






because the anonymous inner class does not have a name, when an anonymous inner class object is created, the new operator is followed by the name of the superclass or interface, and the following {} is the definition of the anonymous class (the creation of the anonymous class object and the declaration of the anonymous class are together).






6. An anonymous class cannot be an abstract class because the system creates an object of an anonymous class immediately when it creates an anonymous class. Therefore, anonymous classes are not allowed to be defined as abstract classes.






You cannot have a constructor for an anonymous class, because an anonymous class has no class name and cannot define a constructor method, but an anonymous class can define an instance initialization block to accomplish what the construction method needs to accomplish by initializing the instance block.






• Anonymous classes cannot define any static members, methods, and classes;






U Anonymous class cannot be public, protected, private, static;






You can only create an instance of an anonymous class.






7. The inner class can be declared static or non-static, or it can be private, protected, public, and the default access control modifiers, called static inner classes, which are decorated with static, and are not decorated with the abbreviation for the inner class.






static inner classes do not have a parent class, and cannot reference external class instance members or other inner class members.






Note:






a static inner class can access any static field or static method of an external class.






U like static methods or static fields, static inner classes have public, private, and default adornments.






U static inner class cannot have the same name as an external class.






As with static methods for external classes, you cannot directly access instance fields and instance methods of an external class.






The static inner class can only be defined in the top-level code of the outer class or the other static member class of the outer class (nested definitions), and cannot be defined in a function of the outer class.






if a static inner class removes the static keyword from its declaration, its definition is moved into the static method of its outer class or the static initialization code snippet, which becomes the local static inner class.






The local static inner class is the same as the basic properties of the static inner class.




Related Article

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.