Core Java (16) internal class

Source: Internet
Author: User

Inner class is a class defined in another class. The internal class is a compiler phenomenon that has nothing to do with the Virtual Machine. the compiler will translate the internal class into a regular class file that separates the external class name from the internal class name with $, and the virtual machine knows nothing about this. The internal analogy is more powerful for conventional classes. The biggest feature is that internal classes can access private data of peripheral classes.

Non-internal class modifiers are only public, abstract & final are permitted, while internal classes can be private.

Internal classes are required for three reasons:

  1. The internal class can access the data in the scope of the class definition, including the private type.
  2. Internal classes can be hidden from other classes in the same package.
  3. It is easier to use anonymous internal classes when you want to define a callback function and do not want to write a large amount of code.
General Writing of internal classes

The following demo demonstrates using an internal class to access private data of an external class.

The internal class pair accesses the private data secret of the external class outerclass. Finally, 100 is output on the console.

package com.xujin;public class Test{public static void main(String...arg){OuterClass outerClass = new OuterClass();outerClass.setSecret(100);System.out.println(outerClass.getInfo());}}class OuterClass{private int secret;public int getInfo(){Pair pair = new Pair();return pair.getSecret();}public void setSecret(int secret){this.secret = secret;}public class Pair{public Pair(){this.take = secret;}public int getSecret(){return this.take;}private int take;}}

The following is the class file after compilation:

The internal class generates a file named outerclass $ pair. Class.

In order to run the above program, the internal class always has an implicit reference, which points to the external class object that creates it. Next, we will display this external class object, which is now called an outer object. The reference of the external class is set in the constructor of the internal class. The following program is exactly the same as the above function, and finally outputs 100 in the console.

package com.xujin;public class Test{public static void main(String...arg){OuterClass outerClass = new OuterClass();outerClass.setSecret(100);System.out.println(outerClass.getInfo());}}class OuterClass{private int secret;public int getInfo(){Pair pair = this.new Pair(this);return pair.getSecret();}public void setSecret(int secret){this.secret = secret;}public class Pair{public Pair(OuterClass outerClass){OuterClass outer = outerClass;this.take = outer.secret;}public int getSecret(){return this.take;}private int take;}}

Local internal class

The local class defined in a method, that is, the local internal class. Its role and limitation are in the block for declaring this.

The local internal class can be completely hidden from the outside. Except for defining the local class method, no other method knows the existence of the local class.

For example, modify the method getinfo of outerclass in the above program to the following:

Public int getinfo () {class pair {// modifier only abstract or final is permittedpublic pair () {This. take = secret;} public int getsecret () {return this. take;} private int take;} pair = new pair (); Return pair. getsecret ();}

Anonymous internal class

If only one object of this class is created, no name is required. This type is called an anonymous class.

Because the anonymous internal class has no name, And the constructor name must be the same as the class name, the anonymous internal class has no constructor.

The anonymous internal class is to expand or implement the parent class or interface, so it can only be

  1. Inherits a class and overwrites the method.
  2. Implement an Interface

The getinfo method in the program outerclass defines an anonymous internal class that implements the pair interface, and calls its getsecret method to return an int value.

package com.xujin;public class Test{public static void main(String...arg){OuterClass outerClass = new OuterClass();outerClass.setSecret(100);System.out.println(outerClass.getInfo());}}class OuterClass{private int secret;public void setSecret(int secret){this.secret = secret;}public int getInfo(){return new Pair(){public int getSecret(){return secret;}private int take;}.getSecret();}}interface Pair{public int getSecret();}

Static internal class

Hides a class inside another class. Internal classes cannot reference external class objects and cancel the generated references.

The compiler automatically adds a reference to the internal class to point to the object of the external class that generates it. If you do not want or need this reference, then we can declare this internal class as static to prohibit the generation of this reference.

In the following example, outerclass. staticinnerclass is a class that can be called by other classes. outerclass. innerclass is not a class that can be initialized with new outerclass. innerclass.

Package COM. xujin; public class test {public static void main (string... arg) {outerclass P = new outerclass (); p. fo (); // COM. xujin. outerclassouterclass. staticinnerclass I = new outerclass. staticinnerclass (p); I. shuchu ("world! "); // Hello, world! I. getclassname (); // COM. xujin. outerclass $ innerclass // outerclass. innerclass Ic = new outerclass. innerclass (); // error, outerclass. innerclass is not a type, similar to static and non-static fields p. new innerclass (). getclassname (); // COM. xujin. outerclass $ innerclass} class outerclass {static class staticinnerclass {staticinnerclass (outerclass) {outer = outerclass;} public void shuchu (string name) {system. out. println ("hello," + name);} public void getclassname () {system. out. println (this. getclass (). getname ();} private outerclass outer;} class innerclass {public void getclassname () {system. out. println (this. getclass (). getname () ;}} public void fo () {system. out. println (this. getclass (). getname ();} private int outerpar ;}

In the following example, The Creation Rules of static and non-static internal classes are provided separately for later viewing.

Package COM. xujin; // The test class is called another public class test {public static void main (string... arg) {// create a static internal class in other classes // staticinnerclass astaticinnerclass = new staticinnerclass (); // you must add outerclassouterclass. staticinnerclass astaticinnerclass = new outerclass. staticinnerclass (); // outerclass. staticinnerclass bstaticinnerclass = new outerclass (). new staticinnerclass (); // you cannot use an instance of an external class to create a static internal class. // create a non-static internal class in another class // outerclass. innerclass ainnerclass = new outerclass. innerclass (); // error. An external class instance must be used to create a non-static internal class outerclass. innerclass ainnerclass = new outerclass (). new innerclass () ;}// static and non-static functions of other classes create internal classes in the same way as public void amethod () {// create a static internal class outerclass in other classes. staticinnerclass astaticinnerclass = new outerclass. staticinnerclass (); // create a non-static internal class in other classes // outerclass. innerclass ainnerclass = new outerclass. innerclass (); // error. An external class instance must be used to create a non-static internal class outerclass. innerclass ainnerclass = new outerclass (). new innerclass () ;}// the outerclass class is an external class outerclass {static class staticinnerclass {} class innerclass {} public void method () {staticinnerclass astaticinnerclass = new staticinnerclass (); // The following two indicate that it doesn't matter if outerclass is not added, and outerclass is compared to other classes (outerclass must be added for other classes. staticinnerclass bstaticinnerclass = new staticinnerclass (); staticinnerclass cstaticinnerclass = new outerclass. staticinnerclass (); innerclass ainnerclass = new innerclass ();} public static void test () {staticinnerclass astaticinnerclass = new staticinnerclass (); // The following two indicate that the addition of outerclass does not matter, compare with other classes (outerclass must be added to other classes) outerclass. staticinnerclass bstaticinnerclass = new staticinnerclass (); staticinnerclass cstaticinnerclass = new outerclass. staticinnerclass (); // innerclass ainnerclass = new innerclass (); // error. You cannot directly create a non-static internal class in a static method. You must have an instance of an external class outerclass. innerclass ainnerclass = new outerclass (). new innerclass ();}}

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.