JAVA 16th (internal classes and their features)
Internal class:
An internal class is a class defined in a class, and the class defined in the class is called an internal class.
I. Access features:
Internal classes can directly access members of external classes, including private members.
To access members of an internal class, an external class must be created.
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();}}
Ii. When should I use it?
It is generally used to analyze things A and finds that things A still describes things B, and finds that things B is still accessing the content of things, then, this transaction B must be defined as an internal class.
For example, when describing the body, there is a brain, and the brain can access other parts of the body, then the brain needs to define internal classes
Iii. Small features of internal classes:
1. Internal classes can be modifier.
Private class in ()
2. directly access internal class members in the external class
If you directly use in BLF = new in ();, this method won't work. Suppose I have another ou2 class whose internal class is also in, then who is this BLF?
Definition method:
Out. in BLF2 = new out (). new in (); // This format is rare. Generally, internal classes are private.
BLF2.showou ();
3. Internal classes are static, so internal classes are equivalent to external classes.
Because an external class is loaded
Class out {private static int num = 3; static class in {public void showou () {System. out. println (num); // The member variables used to access the external class in the 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) {// The internal class is static and the members are non-static out. in BLF = new out. in (); BLF. showou (); // if the internal class is static, the members are also static out. in. showsta ();}}
PS: If a static member is defined in the internal class, the internal class must also be static; otherwise, the compilation fails.
Iv. Internal details:
Why can internal classes directly access members in external classes?
Because the internal class holds the reference of the 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); // It can also be 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: This method is not used in actual development.
5. Local class of internal class
Internal classes can be stored on members or local locations.
Class out {int num = 3; void show1 () {class in {void show () {System. out. print (num) ;}} in blf = new in (); // creates an object, but cannot create blf in an external class. show () ;}} public class Main {public static void main (String [] args) {out blfOut = new out (); blfOut. show1 ();}}
Special case:
1. access local variables from the inside, which must be declared as the final type
That is to say, the internal class can only access local variables modified by final in a local location.
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 following code
We know that all classes inherit the Object class
Class out {int num = 3; Object show1 () // if this method is a method of returning objects {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 (); // blfObject accepts this object, then the show1 method of blfOut is executed. // then it means that the x of show1 is gone, so final must be added, final-MODIFIED x is 5 for life and will not change }}