Inner classes in Java

Source: Internet
Author: User

Classification of inner classes

Characteristics of the Inner class
    1. You can use private, protected adornments. You can also use abstract, final, and other adornments.
    2. Inner classes can access properties and methods of external classes directly or by reference, including private properties and methods (but static inner classes cannot access non-static member variables and methods of external classes). The value of the external property accessed by the inner class is determined by the external class object at the time of construction.
    3. The outer class accesses the members of the inner class only by reference, and can ask all members of the inner class
    4. Inner classes can inherit internal classes from peers or inherit other classes (in addition to inner and outer classes)
    5. An inner class can be defined as an interface, and another class can be defined to implement it
    6. An inner class can be defined as an abstract class, and another inner class can be defined to inherit it
    7. Inner classes within a method cannot be scoped (protected public private), inner classes inside a method cannot have static modifiers, inner classes within a method can only be built within methods, and inner classes within methods, if accessing method local variables, The local variable must use the final decoration
Static Inner class

Test code I

/** * Class definition is added with the static keyword. * cannot have the same name as an external class. * is compiled into a completely separate. class file with the name Outerclass$innerclass.class form. * Only static members and static methods of external classes can be accessed, including private static members and methods. * The way to generate static inner class objects is: * Outerclass.innerclass inner = new Outerclass.innerclass (); */ Public  class staticinnerclasstest {    Private StaticString string ="Hello World";/** * Static inner classes can access static members of external classes * and it can only access static */    Private Static  class staticinnerclass{         Public void Test() {System.out.println (string); }    } Public Static void Main(string[] args) {/** * Regardless of whether private or public is defined, it can be used in external classes */Staticinnerclass Staticinnerclass =NewStaticinnerclass ();    Staticinnerclass.test (); }}

Test Code II

Class Outerclass {Private StaticString string ="Hello World";/** * Static inner classes can access static members of external classes * and it can only access static */    StaticClass Staticinnerclass { Public void Test() {System.out.println (string); }    }} Public  class StaticInnerClassTest2 {     Public Static void Main(string[] args) {/** * When the inner class is public,protected or default, it can be externally defined, * private declared static inner class, cannot be accessed outside. */Outerclass.staticinnerclass Staticinnerclass =NewOuterclass.staticinnerclass ();    Staticinnerclass.test (); }}
member Inner class

Test code

/** * Member Inner classes are also defined in another class, but are not defined with static adornments. * Members inner classes and static inner classes can be analogous to non-static member variables and static member variables. * member Inner class is like an instance variable. * It can access all member variables and methods of its external class, whether static or non-static. */Class Memberinner {Private intD =1;Private intA =2; Public  class memberinnerclass {        Private intA =8; Public void dosomething() {System.out.println (d);/** * Direct access to A, the access is in the inner class of a * /System.out.println (a);How does/** access a in an external class? */System.out.println (Memberinner. This. a); }    }} Public  class memberinnerclasstest {     Public Static void Main(string[] args) {/** * Create an object of a member's inner class * you need to first create an instance of an external class * /Memberinner.memberinnerclass memberinnerclasstest =NewMemberinner ().NewMemberinnerclass ();    Memberinnerclasstest.dosomething (); }}
Anonymous inner class

Test code

ImportJava.util.Date;/** * Anonymous inner class is a local inner class without a name, not using the keyword class, extends, implements, no constructor method. * Anonymous inner class implicitly inherits a parent class or implements an interface. * Anonymous inner classes are used more often as a method parameter. */ Public  class anonymousinnerclasstest {    @SuppressWarnings("Deprecation") PublicStringgetDate(Date date) {returnDate.tolocalestring (); } Public Static void Main(string[] args) {Anonymousinnerclasstest test =NewAnonymousinnerclasstest (); String str = test.getdate (NewDate ()); System.out.println (str);/** * Using the anonymous inner class * uses curly braces, but does not fill in the contents, the execution result and the above exactly the same * generates an object that inherits the subclass of the date class */String str2 = Test.getdate (NewDate () {}); System.out.println (STR2);/** * Use anonymous inner class, and override methods in parent class */String STR3 = Test.getdate (NewDate () {/** * Overriding a method in a parent class */            @Override            @Deprecated             PublicStringtolocalestring() {return "Hello:"+Super. toLocaleString ();        }        });    System.out.println (STR3); }}
Local inner class

Test code

/** * Local inner classes are defined in methods that are smaller than the range of methods. Is the least used type in an inner class. * Like local variables, cannot be modified by public, protected, private, and static. * Only local variables of the final type defined in the method can be accessed. * Local inner classes are defined in methods, so they can only be used in methods, that is, you can only generate instances of local inner classes within a method and invoke their methods. */Class Localinner {intA =1; Public void dosomething() {intb =2;Final intc =3;/** * Defines a local inner class * /Class Inner3 { Public void Test() {System.out.println ("Hello World"); System.out.println (a);/** * Cannot access a non-final local variable, only the final variable is accessible */System.out.println (c); }        }/** * Create an instance of the local inner class and invoke the method */        NewInner3 (). Test (); }} Public  class localinnerclasstest {     Public Static void Main(string[] args) {Localinner inner =NewLocalinner ();    Inner.dosomething (); }}

Copyright NOTICE: Welcome to Exchange

Inner classes in Java

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.