I have written a class named DateUtils where I tried to get resource in a static field like this way
public class DateUtils{
private static String value;
public static string getValue(Context context){
if (value== null) {
value= context.getResources().getString(R.string.my_value);
}
return value; }
}
But I found an answer How can I get a resource content from a static context? where they created an application class and get the reference of this application class to assign value for static field.
In this way the downside is that there is no guarantee that the non-static onCreate() will have been called before some static initialization code tries to fetch your Context object. That means your calling code will need to be ready to deal with null values which sort of defeats the whole point of this question Reference Static way to get 'Context' on Android?
My question is which one is better approach to initialize static field?