Java study note 5 (internal class)

Source: Internet
Author: User

Java study note 5 (internal class)
1. The advantages and disadvantages of an internal class there is another class inside a class, which becomes an internal class. The internal class can be public or private, and its access control is the same as the member variables and member methods of the class. Disadvantages of internal classes: Internal classes actually seriously damage the advantages of good code structure internal classes: convenient access to private properties in external classes; after getting internal classes to external, the code will increase, complexity is also increased; 2. The basic internal class structure copies the code class Outer {private String info = "Hello outer inner class"; class Inner {public void print () {System. out. println (info) ;}} public void fun () {System. out. println ("Outer class fun"); new Inner (). print () ;}} public class InnerDemo {public static void main (String [] args) {// defines the internal class Outer. inner in = null; in = new Outer (). new Inner (); in. print (); new Outer (). fun (); new Outer (). new Inner (). print () ;}} copy code Feature Analysis: when defining an internal class, you must specify the external class to which it belongs. You must first instantiate the external class before instantiating the internal class; 3 static declared internal class copy code class Outer {private static String info = "Hello outer class"; static class Inner {public void print () {System. out. println (info) ;}}; public void fun () {new Inner (). print () ;}} public class InnerDemo {public static void main (String [] args ){ // Define the method Outer. inner in = null; // in = new Outer (). new Inner (); // incorrect method in = new Outer. inner (); in. print (); new Outer. inner (). print () ;}} copy code Feature Analysis: You can directly use external classes. direct access to internal classes has certain limitations. Internal classes can only access static data members of external classes. 4 private internal class copy code class Outer {private String info = "Hello outer class"; private class Inner {public void print () {System. out. println (info) ;}}; public void fun () {new Inner (). print () ;}} public class InnerDemo {public static void main (String [] args) {/* incorrect definition method Outer. inner in = null; in = new Outer (). inner (); in. print (); */new Outer (). fun () ;}} copying code Features Analysis: if an internal class is only intended to be accessed by an external class, you can use private to declare an internal class. An internal class must be generated in the external class. Row operation. new Outer (). new Inner () cannot be used for definition. 5. Copy the code class Outer {private String info = "Hello outer class" in the internal class defined in the class method; // The public void fun () method without Parameters () {class Inner1 {public void print () {System. out. println (info) ;}} new Inner1 (). print () ;}// public void fun (final String str) {class Inner2 {public void print () {System. out. println (info); System. out. println (str) ;}} new Inner2 (). print () ;}}; public class InnerDemo {public static void main (String [] args) {new Outer (). fun (); new Outer (). fun ("zhang") ;}} copying code Features Analysis: an internal class is generated in the external class method to call the methods in the internal class; if the external class method needs to pass parameters, the parameter type must be final.

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.