Static variables in Java

Source: Internet
Author: User

Today I found a problem that is easy to make mistakes and concealed.

 

In androidmenifest. XML, two types of activity A, B, and Cycler C are defined.

A defines a public static variable.

 

A sends a broadcast at a certain time. After the receiver receives the broadcast, it sends an intent to start B.

B changes the value of static variable a during running. When B exits, A is returned. In a, the value of a is read, and the result is still unchanged. That is to say, the value of B's change of A is invalid.

 

I haven't understood it for a long time. Obviously, a is a static variable and directly uses. a's access. After the operation in B, the log has been changed, but the log in a has not changed!

Later I thought of a problem. When I defined a in androidmenifest. XML, I set the Android: Process = "A" value, but not in B. Therefore, Activity A and Activity B run in different process spaces!

 

Static variables are two irrelevant variables in different processes compared to the same process!

 

I would like to remind myself that I encountered a similar problem next time and checked whether I made the same mistake.

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.