title: SQLite database in separate class vs. in same class, which is better? Android
body: <p>I have an SQLite database that is in a separate class from the main class that extends Activity.</p>
  
  <p>I noticed that there are two ways of setting up the database.  one way is to put it inside the main Activity class, either in the class or as a nested sub class.   the second way is to put it in the separate class.</p>
  
  <p>the separate class looks better, however there is one disadvantage.  You have to create an instance of it in the main activity class every time you want to do something.   I read that instantiating objects in Android is expensive and should be avoided.</p>
  
  <p>despite this, I would rather make the database as a separate class.  Is the cost of instantiating objects enough that it makes putting the database in the same class a better deal?</p>
  
  <p>example of separate class for SQLite database: incomplete psudo-code</p>
  
  <pre><code>public class SQLiteDB {
     private static class DbHelper extends SQLiteOpenHelper{
        // db helper methods
     }
  
  // methods for DB, like get, set, and others
  public void openDatabase(){  }
  public void closeDatabse(){  }
  public void insertRecord(String record){  }
  }
  </code></pre>
  
  <p>example use in main Activity:  incompete psudo-code</p>
  
  <pre><code>public class Main extends Activity{
  
  // every time I want to use it I must instantiate an object for the database class
  
  // many instances of SQLiteDB object created, garbage collector works hard
  
  SQLiteDB mDatabase = new SQLiteDB();
  
  openDatabase();
  
  insertRecord("insert this");
  
  closeDatabase();
  
  }
  </code></pre>