Where is sCrimeLab stored when instantiated?


#1

I’m a bit confused, when I was writing Java code I used to declare the singleton object in the singleton class itself. Now in this code

public static CrimeLab get(Context c) if(sCrimeLab == null) sCrimeLab = new CrimeLab(c.getApplicationContext); return sCrimeLab;
sCrimeLab isn’t instantiated in the singleton class but in the “c.getApplicationContext()” (which will be an activity in this case, right?). So, I wanted to ask where this sCrimeLab is exactly stored :question: :question: :question: ? In the host Activity :question: :question: :question: ?


#2

Not clear on the question. The code you listed appears to be the get method from the CrimeLab class which is a singleton. sCrimeLab is a private static field in CrimeLab and it holds the reference to the single instance of that class. The Context reference is passed into the CrimeLab constructor via the get method by the CrimeFragment, CrimeListFragment and CrimePagerActivity clients. The Context object is needed by CrimeLab for instantiating the JSON serializer.

CrimeLab is part of the “Model” layer in the MVC pattern. Clients call its get method to obtain a reference to the single instance of it. They mainly call it to get access to specific crime objects or the entire list of crimes so they can do things like add/save, display, update, and delete crimes, which is the classic CRUD paradyme that is encapsulated by the Model layer. As far as where CrimeLab is actually “stored”, well, electronically it’s stored in the Dalvik heap, but don’t think that’s what you’re asking. In terms of composition, an ArrayList of Crimes is contained in the client objects, which is returned by the CrimeLab singleton via its getCrimes method.


#3

@rcubed, when we access a specific crime in CrimeLab from CrimeFragment, assign it to a local variable and then modify it, how are the changes reflected back in CrimeLab?