[Java Basics] 4. Internal classes in java, internal classes in java

Source: Internet
Author: User

[Java Basics] 4. Internal classes in java, internal classes in java

 

Classification of internal classes: General Internal classes, static internal classes, private internal classes, local internal classes, and anonymous internal classes.

Instance 1: regular internal class
123456789101112131415161718192021222324 // External classclass Out {    private int age = 12;         // Internal class    class In {        public void print() {            System.out.println(age);        }    }} public class Demo {    public static void main(String[] args) {        Out.In in = new Out().new In();        in.print();        // Or use the following method to access        /*        Out out = new Out();        Out.In in = out.new In();        in.print();        */    }}

Running result:12

From the above example, it is not difficult to see that the internal class actually seriously damages the good code structure, but why should we use the internal class?

Because internal classes can use member variables (including private ones) of external classes without generating external class objects, this is the only advantage of internal classes.

Just as the heart can directly access the body's blood, instead of using a doctor to draw blood

 

After the program is compiled, two. class files are generated, namely Out. class and Out $ In. class.

$ Indicates the one In Out. In the above program.

Out. In in = new Out (). new In () can be used to generate internal class objects. This method has two knowledge points.

1. The Out at the beginning is used to indicate the external class of the internal class object to be generated.

2. You must first have an external class object to generate an internal class object, because the internal class is used to access the member variables in the external class.

 

Instance 2: variable access in internal classes
1234567891011121314151617181920 class Out {    private int age = 12;         class In {        private int age = 13;        public void print() {            int age = 14;            System.out.println("Local variable :" + age);            System.out.println("Internal variables :" this.age);            System.out.println("External variables :" + Out.this.age);        }    }} public class Demo {    public static void main(String[] args) {        Out.In in = new Out().new In();        in.print();    }}

Running result:

Local variable: 14
Internal class variable: 13
External variables: 12

From instance 1, we can find that when the internal class does not have member variables or local variables of the same name, the internal class will directly access the member variables of the external class without specifying the Out. this. attribute name

Otherwise, local variables in the internal class will overwrite the member variables of the external class.

The member variables used to access the internal class can be this. attribute name. The Out. this. attribute name must be used to access the member variables of the external class.

 

Instance 3: static internal class
12345678910111213141516 class Out {    private static int age = 12;         static class In {        public void print() {            System.out.println(age);        }    }} public class Demo {    public static void main(String[] args) {        Out.In in = new Out.In();        in.print();    }}

Running result:12

As you can see, if static is used to make the internal static, the internal class can only access the static member variables of the external class, which has limitations.

Second, because the internal class is static, the Out. as a whole, In can be used to directly create internal class objects (accessing static by class name does not matter if an external class object is generated)

 

Instance 4: Private internal class
123456789101112131415161718192021222324 class Out {    private int age = 12;         private class In {        public void print() {            System.out.println(age);        }    }    public void outPrint() {        new In().print();    }} public class Demo {    public static void main(String[] args) {        // This method is invalid        /*        Out.In in = new Out().new In();        in.print();        */        Out out = new Out();        out.outPrint();    }}

Running result:12

If an internal class only needs to be operated by the method in the external class, you can use private to declare the internal class.

In the above Code, We must generate an In class object In the Out class for operations, and cannot use Out. in in = new Out (). new In () generates internal class objects

That is to say, the internal class is only controllable by the external class.

Like, my heart can only be controlled by my body, and others cannot access it directly.

 

Instance 5: Local internal class
1234567891011121314151617181920 class Out {    private int age = 12;     public void Print(final int x) {        class In {            public void inPrint() {                System.out.println(x);                System.out.println(age);            }        }        new In().inPrint();    }} public class Demo {    public static void main(String[] args) {        Out out = new Out();        out.Print(3);    }}

Running result:

3
12

In the above Code, we move the internal class to the external class method, and then generate an internal class object in the external class method to call the internal class method.

If we need to input parameters in the method of the external class, the method parameters of the external class must be defined using final.

As for final, there is no special meaning here, but it is just a representation.

Instance 5: anonymous internal class

[Java Basics] 5. Anonymous internal classes in java

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.