Java-based partial class

Source: Internet
Author: User

Use of internal classes

1 creation of an intrinsic class object:

Non-static inner class: Outer.Inner oi = new Outer (). New Inner ();

Static inner class: Outer.Inner oi = new Outer (). Inner ();

2 method invocations for inner classes

Non-static methods for non-static inner classes and static inner classes: Oi.test ();

static method for static inner class: Inner.test ();

 Public classTest { Public Static voidMain (string[] args) {NewTest ().NewInner1 (). Test1 (); NewTest ().NewInner1 (). Test2 (); Newtest.inner2 (). Test1 (); Newtest.inner2 (). Test2 ();    Inner2.test3 (); }        Private classinner1{ Public voidtest1 () {System.out.println ("Testing internal class public methods"); }                Private voidtest2 () {System.out.println ("Testing internal class private methods"); }        //Public static void Test3 () {//internal classes that contain static methods must be static classes//System.out.println ("Test internal class static Private method");//        }    }        Private Static classinner2{ Public voidtest1 () {System.out.println ("Testing static internal class public methods"); }                Private voidtest2 () {System.out.println ("Testing static internal class private methods"); }                 Public Static voidtest3 () {System.out.println ("Testing static internal class static Private methods"); }    }    }

3. Calls to methods in other classes:

Non-static methods for non-static inner classes and static inner classes: Oi.test ();

static method for static inner class: Outer.Inner.test ();

4. The method inner class, which can only be invoked in the current method, must be created after the class creation

 Public classOuter { Public voidtest1 () {System.out.println ("Test external Class class public methods"); }        Private voidtest2 () {System.out.println ("Test external class private methods"); }         Public voidtest3 () {classinner{ Public voidTest () {System.out.println ("Test method Inner Class"); }        }        NewInner (). Test (); }        classinner1{ Public voidtest1 () {System.out.println ("Testing internal class public methods"); }                Private voidtest2 () {System.out.println ("Testing internal class private methods"); }    }        Static classinner2{ Public voidtest1 () {System.out.println ("Testing internal class public methods"); }                Private voidtest2 () {System.out.println ("Testing internal class private methods"); }                Static voidtest3 () {System.out.println ("Testing internal class static methods"); }    }}

5.1 Inner classes are created in other classes with permission restrictions on calls, but no permission restrictions are invoked in external classes

5.2 Internal classes that contain static methods must be static classes

5.3 The internal class generates a byte file named Outer$inner.class, and the byte file generated by the method inner class is named Outer$1inner.class

Java-based partial 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.