Adapter test; //instance variable
public void Test(){
ArrayList<String> items = new ArrayList<>();
test = new Adapter(items);
}
In the method, items are local variables and
Instance variable test is new Adapter (itmes) in the method; to receive assignments
Test is an instant variable and items is a local variable.
After the method ends, items are local variables, so when memory is released,
Does the memory of the items handed over as a parameter to the instance variable test just disappear?
I think the memory address will remain because it was allocated as new ArrayList<>() anyway.
I'm curious!
Adapter is any adapter
java android
Exiting the Test()
method does not immediately destroy the instance assigned to the instance member. You can refer to it somewhere else.
package misc;
import java.util.ArrayList;
public class TryEverything {
public static void main(String[] args) {
LifeCycle lc = new LifeCycle();
lc.Test();
System.out.println(lc.getTest()); // misc.Adapter@22555ebf
}
}
class LifeCycle {
private adapter test; // instance variable
public Adapter getTest() {
return test;
}
public void Test() {
ArrayList<String> items = new ArrayList<>();
test = new Adapter(items);
}
}
class Adapter {
private ArrayList<String> items;
public Adapter(ArrayList<String> items) {
this.items = items;
}
}
© 2024 OneMinuteCode. All rights reserved.