0

I have an Android App that uses a database. The db class I created will iterate the database and use a seperate class to ?"HOLD"? each row.

public List<Poster> getAllPosters() {
    List<Poster> posterList = new ArrayList<Poster>();
    // Select All Query
    String selectQuery = "SELECT * FROM " + "posters";

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Poster poster = new Poster(Integer.parseInt(cursor.getString(0)),Integer.parseInt(cursor.getString(1)),Integer.parseInt(cursor.getString(2)),cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6),cursor.getString(7),cursor.getString(8));

            // Adding contact to list
            posterList.add(poster);
        } while (cursor.moveToNext());
    }
    db.close(); // Closing database connection
    // return contact list
    return posterList;
}

The Poster Class is as you would expect. It has a constructor with each variable that relates to a column and a method to set and get each column value.

So is each row now it's own object that can be accessed anytime or is it the Poster Class that is the object and it contains all of the rows?

Also, is this object in memory until the app terminates or is overwritten by a new instance of Poster?

silversunhunter
  • 1,219
  • 2
  • 12
  • 32
  • You might want to read about the [difference between classes and objects](http://stackoverflow.com/questions/1215881/the-difference-between-classes-objects-and-instances). – azurefrog Dec 10 '14 at 19:46
  • You return a list of `Poster`. The list and its references are available as long as there's a reference to them. After the last reference is gone it's *eligible* for GC; when that GC occurs depends. – Dave Newton Dec 10 '14 at 19:46

3 Answers3

3

You create multiple Poster objects (instances of Poster class)

Poster poster = new Poster( ... );

and store them in a posterList (a list of Poster objects, List<Poster>)

posterList.add(poster);

and then return the list with

return posterList;

You can use this list to access the retrieved objects with methods like: posterList.get(0)

The created objects will be stored in memory (on heap) until removed by garbage collector. But the garbage collector will not delete anything if you hold a reference to it. In your example, references are held in posterList so no Poster objects will be deleted until you clear the list or remove all references to list itself.

atok
  • 5,880
  • 3
  • 33
  • 62
1

You are returning a list of Poster objectes. Remember, an object is nothing more than an instance of a class. Every time you call Poster poster = new Poster( ... ); you are creating a new object. What you return is a list of these objects you created, which are held on the heap until the program ends, or you overwrite them.

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
  • 1
    `which are held on the heap until the program ends, or you overwrite them.` I know what you mean, but this is not strictly accurate. Any object on the heap becomes eligible for mark and sweep when there are no longer any references to it. You do not have to overwrite them for this to happen. – Simon Dec 10 '14 at 20:32
1
So is each row now it's own object that can be accessed anytime or is it the Poster Class that is the object and it contains all of the rows?

Each row is now in a different level of abstraction, they are now mapped into a Java Object (a Plain Old Java Object [POJO] most likely), also, a "Class" is only a blueprint, it is not an object it wont held anything at all, it will exist as a definition of what an object can be, you need to dive into the basics of this.

Also, is this object in memory until the app terminates or is overwritten by a new instance of Poster?

Yes you are overriding the poster object by erasing his older reference by a new one, but you correctly add the reference of the poster object to the list before you override it, so as long as you use the list, all of those references wont be garbage collected

Ringo
  • 850
  • 9
  • 24