Java internal classes and external classes

Source: Internet
Author: User

1. Use static to declare an inner class that can be called directly outside the

classouter{//defining an external class    Private StaticString info = "Hello world";//define private properties of an external class    Static classinner{//using static to define an inner class as an outer class         Public voidPrint () {//methods for defining inner classesSYSTEM.OUT.PRINTLN (info);//direct access to private properties of external classes        }    };  Public voidFun () {//methods for defining external classes        NewInner (). print ();//invoking a method by instantiating an object from an inner class    }}; Public classinnerclassdemo03{ Public Static voidMain (String args[]) {NewOuter.Inner (). print (); }};

2. Do not use static to declare an inner class so that external calls

classouter{//defining an external class    PrivateString info = "Hello world";//define private properties of an external class    classinner{//Defining inner Classes         Public voidPrint () {//methods for defining inner classesSYSTEM.OUT.PRINTLN (info);//direct access to private properties of external classes        }    };  Public voidFun () {//methods for defining external classes        NewInner (). print ();//invoking a method by instantiating an object from an inner class    }}; Public classinnerclassdemo04{ Public Static voidMain (String args[]) {Outer out=NewOuter ();//External Class Instantiation ObjectOuter.Inner in = out.NewInner ();//instantiating an inner class objectIn.print ();//methods for calling inner classes    }};

3. Define the inner class in the method so that the external call

classouter{//defining an external class    PrivateString info = "Hello world";//define private properties of an external class     Public voidFunFinal intTemp) {//methods for defining external classes        classinner{//inner classes defined in the method             Public voidPrint () {//methods for defining inner classesSystem.out.println ("Properties in class:" + info);//direct access to private properties of external classesSYSTEM.OUT.PRINTLN ("Parameters in method:" +temp);        }        }; NewInner (). print ();//invoking a method by instantiating an object from an inner class    }}; Public classinnerclassdemo05{ Public Static voidMain (String args[]) {NewOuter (). Fun (30);//methods for calling external classes    }};

Java internal classes and external 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.