Java internal class summary, java class Summary

Source: Internet
Author: User

Java internal class summary, java class Summary

Internal classes are not easy to understand, but to put it bluntly, a class also contains another class.

Just as a person is composed of the brain, limbs, organs, and other body results, the internal class is equivalent to one of the organs, such as the heart: it also has its own attributes and behavior (blood, beat)

Obviously, the attribute or method cannot be used to represent a heart, but a class is required.

And the heart is in the human body, just as the internal class is inside the external

 

Instance 1: Basic Structure of internal classes
// External class Out {private int age = 12; // internal 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
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 class variable:" + this. age); System. out. println ("external class variable:" + 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
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
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: Internal method class
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.

 

Internal classes have the following advantages: Internal classes can access private member variables of external classes without the need for new external class objects.
Internal classes are classified into static internal classes, anonymous internal classes, local internal classes, and member Internal classes.

Application scenarios of static internal classes are as follows: only static member variables and static member methods of external classes can be accessed.

The Application Scenario of member Internal classes is: it can access all member variables and methods of its external classes, both static and non-static.

Local internal class: Like local variables, it cannot be modified by public, protected, private, or static. You can only access Partial Variables of the final type defined in the method.

Anonymous internal class: An anonymous internal class is a local internal class without a name. It does not use keywords such as class, extends, and implements. There is no constructor. The anonymous internal class implicitly inherits a parent class or implements an interface. Anonymous internal classes are usually used as a method parameter.

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.