Explanation of Java internal classes and final keywords

Source: Internet
Author: User

Explanation of Java internal classes and final keywords

Several methods for creating internal classes:

1. Member internal class

 
Class outer {private int I = 1; Class inner {public void fun () {system. Out. println ("outer I =" + I )}}}

2. Internal method class

 
Class outer {public void fun () {final int I = 1; // The local variables accessed by the internal class of the method must be modified by final class inner {// The internal class of the method cannot have an access modifier, such as public void print () {system. out. println ("method I =" + I )}}}}

3. Anonymous internal class

 
Interface USB {public abstract void start ()} class Outer {public void fun () {final int I = 1; // local variables accessed by anonymous internal classes must be modified by final new USB () {@ overridepublic void start () {system. out. println ("local_var_ I =" + I );}}. start ();}}

4. static internal class

 
Class outer {private int I = 1; static class inner {// non-static member public void fun () {}} of the external class cannot be accessed (){}}}

5. Interface internal class

 
Interface USB {class inner {// The default value is public static, that is, new USB. Inner ();} can be used directly ();}}

Is Shenma an internal class?

At first glance, it is complicated to create some methods, right? First, what is an internal class?

Definition: The type created inside a class. It is divided into member, method, and anonymous Based on the Creation location. The internal class in the interface is called the internal class of the interface.

Understanding: creating a class is a part of the class, which is similar to attributes and methods. This explains why you can access external private members. I am part of you and certainly can access them.

Problem introduction <understand internal class>:

The question is, I am part of you. If someone else inherits the external class, will they also inherit the internal class? This should be discussed from the original intention of the internal class design and object-oriented.

For example, we use the body class to describe the human body. If it is a description of humans, it will describe the attributes and functions of humans.

So now we describe the human body, the human body has a heart, liver, spleen, and kidney. It is inappropriate to describe these attributes again? What should we do? We can describe it using internal classes.

Therefore, no matter whether the internal class is public or private, it will not be inherited because it is neither an attribute nor a method. It is a description of an internal transaction, which is called an internal class.

Internal classes can better encapsulate internal transactions. For example:

In the body class, there are various organs of the body, each organ has its own functions and attributes, So we encapsulate it into an internal class for separate description.

The internal class used to describe the organ is a part of the body, so you can freely access the body's resources (attributes and functions), and the various organs and the body coordinate to complete the operation.

 
Public class body {private class heart {// heart public void bloodsupply () {// blood supply ...}} private class hepar {// public void metabolism () {// metabolism ...}} private class spleen {// spleen public void storageblood () {// blood storage ...}} private class stomach {// public void Digest () {// digest ...}} private class kidney {// kidney public void dischargepoison () {// detox ...}}}

Problem introduction <internal class access external class member mode>:

Because you can access external private members, the problem arises. How does one access them?

1: for the member internal class, it will hold a reference of the current object of the external class, outer. This.

Then, the internal class uses the static access $0 () method generated by the compiler instead of a direct value when using external class fields.

The call method is called by referencing the current object of the external class held. Outer. This. Fun ();


2: For the internal class of the method (anonymous internal class), can the current object reference of the external class still be called because the internal class needs to access the local variables in the method in which it is located?

If the call fails, the Java Language designers solve this problem: Copy local variables to internal classes. How can I copy them? During Internal class initialization, the value is passed through the constructor.

In this way, the internal class will have a copy of the private modified member variable. In this way, I can access it. But the problem arises again, for example:

 
Public void fun () {int I = 1; Class inner {// int I = 1; the compiler generates public void print () {I ++} system. out. println ("I =" + I); // The output will still be I = 1. We obviously ++ this variable in the internal class method. Sorry, you + are copying another copy .}

Cause of the problem <maintain consistency between two different variables>:

Well, the problem is so heartless. How can this problem be solved? Java also said, otherwise add final to the local variable, so that it will be worth the consistency.

OK. The problem is solved. That is why the local variables accessed by the class inside the method must be modified by final (to constrain the consistency of the two variables ). Design Problems and helpless solutions.

Let's look at another saying:

In Java, local variables of the method are located on the stack, and objects are located on the stack.

Because the range of local variables is limited in this method, when a method ends, the stack structure is deleted and the variable disappears.

However, the internal class objects defined in this class still exist on the stack, so the internal class objects cannot use local variables. Unless these local variables are identified as final.

This statement is one-sided, because the root cause is that internal class objects cannot access local variables, so they will copy a copy.

To ensure the consistency of the two variables, the final keyword is used to modify the local variables. This is not because the stack lifecycle is inconsistent with the stack lifecycle.

Final keywords:

The final keyword is used to modify the object variable, but the object reference is not allowed to point to other objects. However, the content of the object pointed to by this reference can be changed.

For example, a classic example:

 
Final stringbuffer sb = new stringbuffer ("helloworld"); sb = new stringbuffer ("hello"); // compilation failed. You cannot modify the SB reference to sb. append ("China"); // The object to which sb points can be modified.

In fact, we can see from the final position that it is modifying the reference type variable Sb, rather than modifying the instance object in the heap (New stringbuffer ("helloworld ");)

OriginalArticle, Reprinted please indicate the source:Http://blog.csdn.net/thinging_in_android

 

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.