Java Basics-Learning notes (10)--Inner classes (nested classes)

Source: Internet
Author: User

1, the meaning of the inner class

The definition of a class within a class is called a nested class. Nested classes (inner classes) can directly access the member functions and properties of the class (external class) that nest it, even if it is private. But in turn, the external class cannot directly access the members of the inner class.

Other words:

1) Inner classes can access members of external classes directly

2) The outer class cannot directly access the members of the inner class, it needs to first create an object of the inner class, and the object to invoke the members and methods of the inner class.

The definition of an inner class is no different from a normal class, but unlike an external class, it can be modified by protected and private.

1 classOuter2 {3     intOut_i=33;4     voidTest ()5     {6Inner in =NewInner ();7 In.display ();8     }9     classInnerTen     { One         voiddisplay () A         { -System.out.println ("The out_i is" +out_i); -         } the     } - } - classTestouter - { +      Public Static voidmain (String [] args) -     { +Outer obj=NewOuter (); A obj.test (); at     } - } - /* - F:\java_example>java Testouter - The out_i is*/
View Code

First call this display () function to print out the out_i, it first in the display () function to find whether the variable, not to the inner class to find, and finally to outer class to find the variable

2, the use of internal classes

Internal classes make program code more compact and modular.

When the program code in Class A uses the instance object of Class B, and the program code in class B accesses the members in Class A. As the inner class of a, the code is much easier to write.

If we take the inner class inner, directly to the outside, or to implement this function, by generating a inner object in the outer class to invoke its display method, output the outer class member variable out_i. The code looks like this:

classouter{intOut_i=33; voidTest () {Inner in=NewInner ( This);    In.display (); }    }classInner {Outer Outer;  PublicInner (Outer Outer) { This. outer=outer; }        voiddisplay () {System.out.println ("The out_i is" +outer.out_i); }    }classtestouter{ Public Static voidmain (String [] args) {Outer obj=NewOuter ();    Obj.test (); }}/*F:\java_example>java testouterthe out_i is*/
View Code

3. External programs referencing internal classes

1 classOuter2 {3     intOut_i=33;4     voidTest ()5     {6Inner in =NewInner ();7 In.display ();8     }9  Public classInnerTen     { One         intInner_i=3; A         voiddisplay () -         { -System.out.println ("The out_i is" +out_i); the         } -     } - } - classTestouter + { -      Public Static voidmain (String [] args) +     { AOuter obj=NewOuter (); at         //obj.test (); -Outer.Inner in = obj.NewInner (); -System.out.println ("The inner_i_i is" +In.inner_i); -     } - } - /* in F:\java_example>java Testouter - The inner_i_i is 3*/
View Code

An inner class is declared public, an instance object whose outer class outer can be created externally, an instance object of the inner class is created from an instance object of an external class outer, and its members are finally invoked through an instance object of the inner class.

4, internal class with static modification

When the inner class is modified with static, it is equivalent to an outer class, no longer an inner class

5. Internal classes can be defined not only within a class, but also within the scope of several program blocks. For example, in a method, even inside a for loop body can be nested

The inner class defined in the method can only access local variables of the final type in the method, because the scope of the local variable is only within this method, and when the external program is called, the local variable memory is retracted, so the final modification is required, and the final defined local variable is equivalent to a constant. Its life cycle is beyond the life cycle of method operation

1 classOuter2 {3     intOut_i=33;4     voidTest ()5     {6         Final intx=2;7         classInner8         {9             intInner_i=3;Ten             voiddisplay () One             { ASystem.out.println ("The Out_i is" +out_i+ "and X is" +x); -             } -         } theInner in =NewInner (); - In.display (); -     } -  + } - classTestouter + { A      Public Static voidmain (String [] args) at     { -Outer obj=NewOuter (); - obj.test (); -     } - } - /* in F:\java_example>java Testouter - The out_i is, and x is 2*/
View Code

Java Basics-Learning notes (10)--Inner classes (nested classes)

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.