Java Basics Java Inner class

Source: Internet
Author: User

What is an internal class

The class is defined inside other classes and is called an inner class.

Classification of inner classes

The inner class is divided into two types, the member inner class and the local inner class, respectively:

    1. Members Inner classes: and member variables and member methods defined in siblings
    2. Local inner classes: and local variables are defined at the sibling, including in the constructor method, member method body, and static method body
classOuter {classInnera {//member Inner class    }            {        classInnerb {//local inner class        }    }    Static {        classINNERC {//local inner class        }    }     PublicOuter () {classInnerd {//local inner class        }    }     Public voidShow () {classInnerE {//member Inner class        }    }}

member internal class access characteristics
    1. An inner class can access all members of an external class directly.
    2. An external class must create an object to access the members of the inner class.
    3. Member inner classes are generally more private and static decorated
classOuter {Private intAge = 20; classInner { Public voidGetage () {System.out.println (age); }    }         Public voidmethod () {//symbol not found//getage ();Inner Inner=NewInner (); Inner.Getage();
}
}
 Public class Test {    publicstaticvoid  main (string[] args) {        new Outer (). New Inner ();        Inner.getage ();    }}

In the example above, you can directly pass new Outer (). New Inner (); To access the internal, this kind of access is generally unsafe, because the class is defined internally without being externally defined, there must be security requirements, which can be modified before the inner class multibyte private, if the member variable in the class, the external can only be accessed by the specified method, This time in the external method can provide validation, for example:

classOuter {Private classInnera { Public voidPlay () {System.out.println ("Play"); }    }     Public voidGetinner (String name) {if(Name.equals ("Lili")){            NewInnera (). Play (); }    }} Public classTest { Public Static voidMain (string[] args) {Outer Outer=NewOuter (); Outer.getinner ("Lili"); }}

Of course, member inner classes also use static modifiers, which makes access easier, but static inner classes can only access static external member variables .

 Public classTest { Public Static voidMain (string[] args) {Outer.Inner Inner=NewOuter.Inner ();    Inner.getage (); }}classOuter {Private Static intAge = 20; Static classInner { Public voidGetage () {System.out.println (age); }    }     Public voidmethod () {Inner Inner =NewInner ();    Inner.getage (); }}

Local internal class Access features
    1. All members of an external class can be accessed directly
    2. In a local location, you can create an inner class object to use the functionality provided by the local inner class
    3. Local internal class access local variables must be final decorated

classOuter {PrivateString name = "Lili";  Public voidShow () {intPhonenum = 10086; classInner {Final intAge = 20;  Public voidGetage () {//phonenum =;//Error: (+) Java: The local variable referenced from the inner class must be the final variable or the actual final variable                intMiniphonenum = Phonenum;//can accessSystem.out.println (age); System.out.println (phonenum);System.out.println (name); }        }        NewInner (). Getage (); }} Public classTest { Public Static voidMain (string[] args) {Outer Outer=NewOuter ();    Outer.show (); }}

Why can local inner classes only access local variables of the final type?

A local variable is called as the method is called, and disappears as the call finishes. The contents of the heap memory do not disappear immediately. So, we add final retouching. When the final modifier is added, the variable becomes a constant. Since it is a constant. You're gone, I'm storing data 20 in memory, so I still have the data in use.

How to distinguish multi-layer variable with same name in inner class
classOuter { Public intnum = 10; classInner { Public intnum = 20;  Public voidShow () {intnum = 30;            SYSTEM.OUT.PRINTLN (num); System.out.println ( This. Num); //System.out.println (New Outer (). num);System.out.println (Outer. This. Num); }    }}

Java Basics Java Inner class

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.