What are the disadvantages of using static variables in Android programming? How should it be regula

Updated on technology 2024-03-31
4 answers
  1. Anonymous users2024-02-07

    In fact, whether to use static or not, it completely depends on the applicable environment, and it is good to use it if it is not good.

    static can only work within a process, cross-process is useless.

    In most cases, if you share data between multiple activity services in the same application, you can consider using static variables, but the other option is to put them in the application, and the effect is similar in most cases. But in general, static is used to reduce the number of variables, while non-static needs to use some singleton pattern to store variables.

    Generally speaking, if there is a problem with multi-threaded access, pay attention to the lock.

    In addition, the biggest use of static is the constant of the const class such as static final.

  2. Anonymous users2024-02-06

    Static variables have the following disadvantages:

    1. Static local variables are allocated storage units in the static storage area. It is not released during the entire running period of the program, that is, it takes up a lot of space.

    2. Dynamic data cannot be saved.

    3. Static variables are easy to cause confusion when multiple forms are accessed together, and they are not independent of each other when they are passed between multiple instances of multiple form classes, and it is more resource-saving when they are frequently accessed.

    4. Static variables are stored in the heap area of memory, which can be saved until the end of the whole program, and the life cycle is long.

    It's okay to use static variables sparingly, but not everywhere.

  3. Anonymous users2024-02-05

    1. Static application scope: It can be used for internal classes, methods and variables.

    2. Mark a method as static, which means that this method is shared by all instances of the class in which it is located, and is executed first when the class is loaded, and is shared by all instances of the class when it is executed once, which also means:

    a. Non-static variables cannot be referenced inside the static method.

    b. Static methods cannot be rewritten as non-static methods by subclasses.

    c. The non-static method of the parent class cannot be overridden as a static method by the child class.

    d. The static block can be done with static {}, which is initialized when the class is loaded for the first time, before the execution of static methods and other methods.

    3. Mark a variable as static, then the variable has an unchanged position in memory, which is equivalent to a global variable, and all instances of the class access the same storage variable area. Modifications to it are visible and consistent for all instances of the class.

    The disadvantage is obvious, if it doesn't play a reasonable role it takes up a lot of space and won't be automatically taken up by the **mechanism until the end of the program.

    When using it, you should make sure that this variable is used frequently, and you can use static variables to avoid repeated instantiation and unnecessary overhead.

  4. Anonymous users2024-02-04

    This mainly depends on the design of **, and there are no major shortcomings. Static variables, once initialized, allocate memory and are held by the object until the object is **. If it is a public static variable, even if the object is **, the variable will already be in memory and occupy memory.

    If it is private static, the object that holds the static variable will be cleared after the object is **. Half of the objects in Android development should not be defined as static, such as context, when the activity is finished, the static context will still exist, but it is not released, which may cause OOMIn short, there are no disadvantages to static variables, it mainly depends on the project requirements and your ** design.

    My approach is that if I need to define an object with static variables and more than I need, I define a singleton pattern. If it's a normal variable, like final static int flag = 1, I'll define it in a constant class.

Related questions
5 answers2024-03-31

1.The easiest way:

public static string reverse1(string str) >>>More

15 answers2024-03-31

Note that the getline function uses a carriage return as a terminator, but it does not take a carriage return into account. in the first, 5); At the end of the end, a carriage return is kept in the cache when executed to the second, 5); The function reads the carriage return that is kept in the cache, and thinks that the read has ended, so the second string of five characters entered after that is not read into ch, of course, ch is empty, and can be used; Solve this problem; Here's how: >>>More

6 answers2024-03-31

Enter the commands in turn.

1. i inserts i at the current position before death, a at the beginning of the current line, a after the current position, o at the end of the current line, o after the current line, and a line before the current line. >>>More

5 answers2024-03-31

There is not much difference.

MID is the meaning of mobile Internet device, which is a new concept of mini laptop launched by Intel at the 2008 IDF conference. In Intel's definition, this is a mobile Internet device that is smaller than a laptop but larger than a mobile phone. MID is similar to UMPC in that it is also a portable mobile PC product, through which users can access the Internet and enjoy entertainment, information inquiry, email sending and receiving at any time. >>>More

25 answers2024-03-31

In ICL lens implantation surgery, an intraocular lens (ICL) needs to be implanted into the eye, located behind the iris, and fixed in the ciliary sulcus to maintain the natural adjustment function of the lens and play a role in correcting myopia. Since the material of the intraocular lens used in ICL lens implantation surgery is very close to the natural lens of the human being, and each intraocular lens is "tailor-made" according to the eyes of the myopic person, once the ICL is implanted in the eye, it is easy to be accepted by the human body, and others or the surgeon himself can neither see nor feel the lens. All intraocular lenses implanted in the eye can be tubed for a lifetime.