In programming languages that have garbage collector, what is the best way to access hard coded variables and objects. I have 2 options:
OPTION 1:
class Hello {
void retrieveData() {
Map map = App.staticMap;
}
}
public class App {
public static Map map = new Map<>() {
"a", "b",
"b", "d",
//hundreds of lines
}
}
OPTION 2:
class Hello {
void retrieveData() {
//let assume App instance is declared when program is started.
Map map = App.getInstance().dataMap;
}
}
public class App {
private static App instance;
Map dataMap = new Map() <> {
//hundreds of lines
}
public static App getInstance() {
return instance;
}
}
I couldnt find best way because of garbage collector. When i call static variable, maybe garbage collector collected it before calling. I guess the same thing is valid for instance object. Which one is the best way in JAVA and C# ? or are there options?