Summarize the life cycle of Java objects to be aware of in Android development

Lifecycle concept in Android development

In iOS, the view controller and view class have the concept of life cycle, It was very complicated to set the auto layout. So I was wondering what about Android. It was known that Android also has a life cycle in Activity, For the first time today, I learned that language specifications also have a life cycle.

Maybe I wasn't even aware of Objective-C or Swift, There may have been an object life cycle.

Therefore, I will summarize the life cycle in the mJava language.

3 types of Java life cycle

There are roughly three types of object life cycles.

--Local variables --Instance variables --Class variables

Local variables

Local variables are variables that can only be used within a block of processing.

LocalVar.java


int sampleMethod() {
    int count = 0; //Local variables
    return count;
}

It is generated at the place where the variable is declared, and is destroyed when the block processing is completed.

Instance variables

Instance variables are variables that are declared as fields in a class. The life cycle of an instance variable is the same as the life cycle of an instance of a class, Created when the parent object is created and destroyed when the parent object is garbage collected. Garbage collection is a mechanism that releases unnecessary areas of the memory area allocated by the program.

Class variables

A class variable is a variable that is declared as a static field in a class. Has the longest life cycle. It is created when the class is loaded and destroyed when it is destroyed.

LifeCycleSample.java


public class LifeCycleSample {
  public static int classVariable = 1; //Class variables
  public int instanceVarialbe = 1;     //Instance variables

  public void sampleMethod() {
    int localVariable = 1;            //Local variables
    return localVariable;
  }
}

Life cycle good practice

There are roughly two patterns of good practices for declaring variable objects.

  1. Shorten the life cycle.
  2. Extend the life cycle and improve performance.

I myself have tried to minimize the scope by declaring variables where necessary in the field so far. If you hold it for too long, the variable will be called from another place at an unintended timing and the state will change (laughs). It is clear that unintended programs can cause bugs and should be avoided as much as possible.

So, for me, I think it's easy to do a pattern that shortens the life cycle of 1.

Recommended Posts

Summarize the life cycle of Java objects to be aware of in Android development
Things to be aware of when writing code in Java
Android development, how to check null in the value of JSON object
Things to be aware of when writing Java
Summarize the life cycle of Java objects to be aware of in Android development
Organizing the current state of Java and considering the future
Monitor the internal state of Java programs with Kubernetes
Be aware of garbage collection and avoid memory leaks
I want to be aware of the contents of variables!
I tried to summarize the methods of Java String and StringBuilder
[Java] Things to be aware of when outputting FizzBuzz
I want to be aware of the contents of variables!
Summarize the additional elements of the Optional class in Java 9
[Java] [Microsoft] Things to be aware of when including the JDBC driver for SQL Server in one jar
About the Android life cycle
I tried to summarize the basics of kotlin and java
Refer to C ++ in the Android Studio module (Java / kotlin)
[Android, Java] Convenient method to calculate the difference in days
[Java Silver] Things to be aware of regarding switch statements
How to implement one-line display of TextView in Android development
How to derive the last day of the month in Java
[Rails] Where to be careful in the description of validation
Be sure to compare the result of Java compareTo with 0
About the basics of Android development
To be aware of easy-to-read code
A newcomer tries to summarize the Android view (beginner Android application development)
Output the difference between each field of two objects in Java
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
I tried to summarize the methods of Java String and StringBuilder
Determine if the strings to be compared are the same in Java
How to get the length of an audio file in java
How to increment the value of Map in one line in Java
Get the result of POST in Java
Java reference to understand in the figure
The story of forgetting to close a file in Java and failing
[Java] Equivalence comparison where beginners fail in string comparison. You may not even be aware of the mistake! ??
I tried to summarize the key points of gRPC design and development
Library summary that seems to be often used in recent Android development (2019/11)
What wasn't fair use in the diversion of Java APIs on Android
Easily measure the size of Java Objects
Reason to add L to the number to be put in Java long type
How to find the total number of pages when paging in Java
How to get the date in java
Output of the book "Introduction to Java"
How to get the absolute path of a directory running in Java
The story of writing Java in Emacs
Java to C and C to Java in Android Studio
[Beginner] Points to be aware of after Java exercises / Inheritance / Abstract method [Note 26]
Make the JSON of the snake case correspond to the field of the camel case in Java (JVM)
Let's refer to C ++ in the module of AndroidStudio other project (Java / kotlin)
[Java] Is it unnecessary to check "identity" in the implementation of the equals () method?
Conditional branching of the result of SQL statement to search only one in Java
I stumbled on the Java version in Android Studio, so I will summarize it
Sample code to get the values of major SQL types in Java + MySQL 8.0
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
[Android] [Java] Manage the state of CheckBox of ListView
About the idea of anonymous classes in Java
CORBA seems to be removed in Java SE 11. .. ..
The story of learning Java in the first programming
Measure the size of a folder in Java
Feel the passage of time even in Java
There seems to be no else-if in java
[Java] Be aware of short circuits (short-circuit evaluation)
Minecraft BE server development from PHP to Java
Java Servlet should be aware of multithreaded environment