Java Learning Lesson 16th (inner class and its characteristics)

Source: Internet
Author: User

Inner class:

An inner class is a class defined in a class, and a class defined in a class is called an inner class.

First, the characteristics of the visit:

Inner classes can access members of external classes directly, including private members
External class to access members of an inner class, you must create an object of the inner class

Simple embodiment:

Import out.in;class out{private int num  = 3;class in{public void Showou () {System.out.println (num);}} public void Showin () {In BLF = new in (); Blf.showou ();}} public class Main{public static void Main (string[] args) {out BLF = new Out (); Blf.showin ();}}

second, when to use?


Generally used to analyze things a, found that the thing a description also has the thing B, found that the thing B is also accessing the content of the described thing a, then this thing B must be defined as an internal class

For example: When describing the body, and the brain, while the brain can also access other parts of the body, the brain will define internal classes

third, the small characteristics of the inner class:


1. Inner classes can be decorated with modifiers.


Private class in ()

2. Direct access to internal class members in external classes
If the direct in BLF = New in (), this way is not possible, assuming I have a OU2 class his inner class is also in, and whose is this BLF?
How to define:
Out.in BLF2 = new Out (). New in ();//This format is rare, general internal classes are private
Blf2.showou ();




3. If the inner class is static, then the inner class is equivalent to the outer class
Because an external class is loaded, there is a

class out{private static int num  = 3;static class in{public void Showou () {System.out.println (num);// The member variable that accesses the external class in a static class must also be static}public static void Showsta () {System.out.println ("STA show" +num);}} public void Showin () {In BLF = new in (); Blf.showou ();}} public class Main{public static void Main (string[] args) {//inner class is static, member is non-static out.in BLF = new out.in (); Blf.showou ();//If the inner class is static, the member is also a static condition Out.in.showsta ();}}

PS: If a static member is defined in an inner class, the inner class must also be static or the compilation fails


Iv. Internal class Details:


Why does an inner class have direct access to members in an external class?
Because an inner class holds a reference to an external class, the external class name. This

class out{int num  = 3;class in{int num = 4;public void Showou () {int num = 5; SYSTEM.OUT.PRINTLN (num);//If you want to print 4,this.numsystem.out.println (this.num), or System.out.println (In.this.num); If you want to print 3system.out.println (Out.this.num);}} public void Showin () {In BLF = new in (); Blf.showou ();}} public class Main{public static void Main (string[] args) {out.in BLF = new Out (). New in (); Blf.showou ();}}

PS: Actual development does not appear in this notation


local classes of internal classes


The inner class can be stored in a local position in addition to the member.

Class Out{int num = 3;void Show1 () {Class in{void show () {System.out.print (num);}} In BLF = new in ();//Create object, but cannot create blf.show () in external class;}} public class Main{public static void Main (string[] args) {out blfout = new Out (); Blfout.show1 ();}}

Exception:


1. Accessing local variables from within, need to be declared as final type
In other words, the inner class can only access locally-modified local variables in local locations

Class Out{int num = 3;void show1 () {final int x = 5;class in{void Show () {System.out.println (x);}} In BLF = new in (); Blf.show ();}} public class Main{public static void Main (string[] args) {out blfout = new Out (); Blfout.show1 ();}}

Why is it defined as final?

See the code below

We know that all classes inherit the object class.


Class Out{int num = 3;object show1 ()//If this method is a method that returns an object {int x = 5;class in{void Show () {System.out.println (x);}} Object BLF = new in ();//blf.show (); return BLF;}} public class Main{public static void Main (string[] args) {out blfout = new Out (); Object blfobject = Blfout.show1 ();//blfobj ECT accepts this object, then Blfout's Show1 method executes                 //Then means that Show1 x is gone, so the x that must be added final,final modified is 5 for life and will not change}}


Java Learning Lesson 16th (inner class and its characteristics)

Related Article

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.