JAVA----method of inner class (external invoke method of inner class) __java

Source: Internet
Author: User

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

[Java]  View plain  copy class outer{        //  define outer class         private static String info =  "Hello world"  ;     //  define private properties for external classes        static class inner{  //  use static to define inner class as outer class            public void  print () {                   Methods for defining internal classes                 SYSTEM.OUT.PRINTLN (Info)  ;      //  Direct access to the private properties of external classes             }       };        public void fun () {                       //  methods for defining external classes             new inner (). Print ()  ;                //  invoking methods via internal class-instantiated objects        }  };   public  class InnerClassDemo03{       public static void main ( String args[]) {           new outer.inner (). Print ()  ;                 / /  calls the fun () method of the outer class        }  };  
2. Do not use STATC to declare an inner class, so that the external call

[Java]  View plain  copy class outer{        //  define outer class         private String info =  "Hello world"  ;    //  define private properties for external classes        class Inner{    //  Define inner class            public void print () {                 //  ways to define inner classes                 SYSTEM.OUT.PRINTLN (info)  ;       //  direct access to private properties of external classes             }       };       public void fun () {                        //  defines the method of the outer class            new inner (). Print ( )  ;               //  Object Invocation Method        }  };   public class  by internal class instantiation innerclassdemo04{       public static void main (String args[) ) {           outer out = new outer ()  ;           //  External Class instantiation object             outer.inner in = out.new inner ()  ;   //  instantiation of internal class objects            in.print ()  ;                          //&nbsp Method of calling Inner class        }  };  

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

[Java] view plain Copy class outer{//define an external 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.