Java Learning Note Five (inner class)

Source: Internet
Author: User

1 advantages and disadvantages of internal classes

There is another class inside a class that becomes an inner class. An inner class can be public or private, with the same control over its access as the member variables and member methods of the class.

Disadvantages of inner classes: internal classes actually seriously undermine the good code structure

Advantages of inner classes: easy access to private properties in external classes; After taking the inner class outside, the code will increase and the complexity will increase;

2 Basic internal class structure

classouter{PrivateString info = "Hello outer inner class"; classinner{ Public voidprint () {System.out.println (info); }    }         Public voidFun () {System.out.println ("Outer class Fun"); NewInner (). print (); }} Public classInnerdemo { Public Static voidmain (String [] args) {//Defining inner ClassesOuter.Inner in =NULL; Inch=NewOuter ().NewInner ();        In.print (); NewOuter (). Fun (); NewOuter ().NewInner (). print (); }}

Feature Analysis: When defining an inner class, you must specify the external class to which it belongs, and you must instantiate the class before you can instantiate the inner classes.

3 inner class of the static declaration

classouter{Private StaticString info = "Hello outer class"; Static classinner{ Public voidprint () {System.out.println (info);        }    };  Public voidFun () {NewInner (). print (); }    } Public classinnerdemo{ Public Static voidMain (String []args) {//How to defineOuter.Inner in =NULL; //In = new Outer (). New Inner ();//the wrong wayinch=NewOuter.Inner ();        In.print (); NewOuter.Inner (). print ();}}

Feature analysis: Can be directly through the external class. Internal classes are directly accessible, with some limitations, and internal classes can only access static data members of external classes.

4 Private Inner class

classouter{PrivateString info = "Hello outer class"; Private classinner{ Public voidprint () {System.out.println (info);        }    };  Public voidFun () {NewInner (). print (); }    } Public classinnerdemo{ Public Static voidMain (String []args) {/*Definition Method Error outer.inner in = null; in = new Outer ().        Inner ();        In.print (); */                NewOuter (). Fun ();}}

Feature Analysis: If an inner class only wants to be accessed by an external class, the inner class can be declared using private, and an inner class must be generated in the outer class for operation and cannot be defined using new Outer (). New Inner ().

5 inner classes defined in the methods of the class

classouter{PrivateString info = "Hello outer class"; //a method without parameters     Public voidFun () {classinner1{ Public voidprint () {System.out.println (info); }        }                NewInner1 (). print (); }        //Methods with Parameters     Public voidFunFinalString str) {        classinner2{ Public voidprint () {System.out.println (info);            System.out.println (str); }        }                NewInner2 (). print (); }    }; Public classinnerdemo{ Public Static voidMain (String []args) {NewOuter (). Fun (); NewOuter (). Fun ("Zhang"); }}

Feature Analysis: An inner class is generated in the method of the outer class to invoke the method in the inner class, and if a parameter is passed in the method of the outer class, the type of the parameter must be final.

Java Learning Note Five (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.