Re-entry into the program, the different manifestations of Java static variables and member variables and the thinking of singleton patterns

Source: Internet
Author: User

We know that, in storage, member variables are stored inside the heap in the JVM, while static variables are in the method area of the JVM, the main difference being that the member variables are no longer referenced and often result in recycling, and the method area is very restrictive for garbage collection, unless the entire application is unloaded. Otherwise, the static variable will persist.

Using member variables in Android apps, all member variables are definitely recycled after exiting the app, whereas static variables are reversed. Especially in UI design, if you customize a static ViewGroup, create the time for its addchlid, you have to consider this situation, because again into the program, or will call Addchild method, this will definitely error, if the child is dynamically created, So every time ViewGroup will have a new member to fill in, if child is a member variable or something, because this variable has a static reference exists, so it will not be eliminated, added again will be reported to child can only has a parent error. In another case, if child is also a static variable, then more than one static variable, the memory overhead ... Especially on mobile phones.

In summary, in the UI design to define a singleton mode, feel less reliable, unless there is no sub-component, or to avoid reporting to the child can only has a parent error, to constantly judge whether the children have a father, father, from the father to remove the child (because there is always a static father quoted you).

Re-entry into the program, the different manifestations of Java static variables and member variables and the thinking of singleton patterns

Related Article

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.