Java know how much (37) static inner class, anonymous inner class, member inner class, and local inner class

Source: Internet
Author: User
Tags control characters modifiers

The inner classes can be static (static) and can use public, protected, and private access controls, while external classes can use only public, or default.
A class that is directly defined within a member's inner class (not inside a method or inside a code block) is a member inner class that can directly use all the variables and methods of an external class, even if it is private. External classes to access the member variables and methods of the inner class, you need to get through the object of the inner class.

Take a look at the following code:
1  Public classouter{2     Private intsize;3      Public classInner {4          Public voidDostuff () {5size++;6         }7     }8      Public voidTesttheinner () {9Inner in =NewInner ();Ten In.dostuff (); One     } A}
A member inner class is like an ordinary member of an outer class.

Member-style inner classes can use various modifiers, including public, protected, private, static, final, and abstract, or not.

If there is a static modifier, it is class-level, otherwise the object-level. The class level can be accessed directly through an external class, which needs to be accessed by an object-level object.

You cannot declare any static member in a non-static inner class.

Inner classes can be called to each other, for example: access to member-internal classes
1 class A {2     // B, C can call each other 3     class B {} 4     class C {} 5 }
An object of an inner class records a reference to its dependent outer class object as a member variable, so it can find the outer class object and access its members. This member variable is automatically added by the system to a non-static inner class, and the name convention is "Outclassname.this".

1) When using non-static variables and methods defined in the inner class, create an object of the outer class, and then create an object of the inner class by the "Outobjectname.new" operator, and then call the method of the inner class as follows:
1  Public classdemo{2      Public Static voidMain (string[] args) {3Outer Outer =NewOuter ();4Outer.Inner Inner = Outer.NewInner ();5 Inner.dostuff ();6     }7 }8 classouter{9     Private intsize;Ten     classinner{ One          Public voidDostuff () { Asize++; -         } -     } the}
2) The static inner class is equivalent to the static member of its outer class, and its object is not dependent on the Outer class object, so it can be created directly. Examples are as follows:
1  Public classdemo{2      Public Static voidMain (string[] args) {3Outer.Inner Inner =NewOuter.Inner ();4 Inner.dostuff ();5     }6 }7 classouter{8     Private Static intsize;9     Static classInner {Ten          Public voidDostuff () { Onesize++; ASystem.out.println ("size=" +size); -         } -     } the}
Operation Result:
Size=1

3) because an inner class has direct access to the components of its outer class, a naming conflict can also result when an inner class has a property or method with the same name in its outer class. So in a multi-layered invocation, specify as follows:
1  Public classouter{2     Private intsize;3      Public classinner{4         Private intsize;5          Public voidDostuff (intsize) {6size++;//local variable size;7              This. size;//size of the inner class8Outer. This. size++;//size of the outer class9         }Ten     } One}
Local inner class local inner classes are classes that are defined in a code block. They are visible only in the block of code in which they are defined.

Local classes have several important features:
    1. are visible only in the block of code in which they are defined;
    2. You can use any local final variable in the code block in which they are defined;
    3. A local class is not static, and static members cannot be defined inside;
    4. Local classes can not be modified with public, private, protected, can only use the default;
    5. The local class can be abstract.

Take a look at the following code:
1  Public classOuter {2      Public Static Final intTotal_number = 5;3      Public intID = 123;4      Public voidfunc () {5         Final intAge = 15;6String str = "Http://www.weixueyuan.net";7         classInner {8              Public voidinnertest () {9 System.out.println (total_number);Ten System.out.println (ID); One                 //System.out.println (str), non-method, access only the final variable of the local method A System.out.println (age); -             } -         } the         NewInner (). Innertest (); -     } -      Public Static voidMain (string[] args) { -Outer Outer =NewOuter (); + Outer.func (); -     } +}


Operation Result:
5
123
15
Anonymous inner class an anonymous inner class is a special form of a local inner class that does not have a variable name pointing to an instance of the class, and the specific class implementation is written inside the inner class.

Note: An anonymous class must inherit a parent class or implement an interface.

Do not use anonymous inner classes to implement abstract methods:
1 Abstract classPerson {2      Public Abstract voideat ();3 }4 classChildextendsPerson {5      Public voideat () {6System.out.println ("Eat Something");7     }8 }9  Public classDemo {Ten      Public Static voidMain (string[] args) { OnePerson p =NewChild (); A p.eat (); -     } -}
Operation Result:
Eat something

As you can see, we inherit the person class with child, and then we implement an instance of child and transform it upward into a reference to the person class. However, if the child class here is used only once, wouldn't it be a hassle to write it as a separate class?

The anonymous inner class is introduced at this time. Using an anonymous inner class implementation:
1 Abstract classPerson {2      Public Abstract voideat ();3 }4  Public classDemo {5      Public Static voidMain (string[] args) {6        7         //Inherit Person class8         NewPerson () {9              Public voideat () {TenSystem.out.println ("Eat Something"); One             } A }.eat (); -     } -}

As you can see, the anonymous class inherits the person class and implements the method of the abstract class in curly braces.

The internal class syntax is more complex, the actual development is also less used, this tutorial is not intended to carry out in-depth explanation, readers should not be the inner class as the focus of learning Java.

Series Articles:

Java know how much (1) Language overview

Java know how much (2) virtual machine (JVM) and cross-platform principle

Java know how much (3) employment direction

Java know how much (4) the difference between J2SE, Java EE, J2ME

Java know how much (5) Build Java development environment

Java know how much (6) The first example of a program

Java knows how many (7) classes and objects

Java know how much (8) class library and its organizational structure

Java know how much (9) Import and Java class search path

Java know how much (10) data types and variables

Java know how much (11) data type conversions

Java know how many (12) operators

Java know how much (13) Process Control

Java know how many (14) arrays

Java know how much (15) string

Java know how much (StringBuffer) and Stringbuider

Java know how much (17) emphasize the programming style

Java know how many (18) classes are defined and instantiated

Java know how many (19) access modifiers (access control characters)

Java knows how many (20) variables are scoped

Java know how much (+) This keyword is detailed

Java know how many (22) method overloads

Java know how much (23) the basic run order of classes

Java know how much (24) packaging class, unpacking and packing detailed

Java know how much (25) More about Java package

Java know how much (26) claim rules for source files

Java know how much (27) the concept and implementation of inheritance

Java know how many super keywords

Java know how much (29) Overwrite and reload

Java know how much (30) polymorphic and dynamic binding

Java know how many static keywords and Java static variables and static methods

Java know how much (instanceof)

Java know how much (33) type conversions for polymorphic objects

Java know how much (final keyword): Block inheritance and polymorphism

Java knows how many () object classes

Java know how much (36) inner class and its instantiation

Java know how much (37) static inner class, anonymous inner class, member inner class, and local inner class

Java know how much (37) static inner class, anonymous inner class, member inner class, and local 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.