java - Cannot use 'this' in static context -


this question has answer here:

my aim have class containing functions perform database operations cleanly , neatly - can called single line of code, eg; dbfunctions.addcontact("fname", "lname");

i have dbadapter class have read in tutorial:

public class dbadapter { static final string key_rowid = "_id"; static final string key_name = "name"; static final string key_email = "email"; static final string tag = "dbadapter";  static final string database_name = "mydb"; static final string database_table = "contacts"; static final int database_version = 1;  static final string database_create =         "create table contacts (_id integer primary key autoincrement, "         + "name text not null, email text not null);";  final context context; databasehelper dbhelper; sqlitedatabase db;  public dbadapter(context ctx) {     this.context = ctx;     dbhelper = new databasehelper(context); }  private static class databasehelper extends sqliteopenhelper {     databasehelper(context context)     {         super(context, database_name, null, database_version);     }      @override     public void oncreate(sqlitedatabase db)     {         try         {             db.execsql(database_create);         }         catch (sqlexception ex)         {             ex.printstacktrace();         }     } }  //---opens database--- public dbadapter open() throws sqlexception {     db = dbhelper.getwritabledatabase();     return this; }  //---closes database--- public void close() {     dbhelper.close();        }     // other database functions here... inserts, updates etc } 

and have created own class handle calls dbadapter:

    public static class databasesactivity extends activity {            static dbadapter db;      // called when activity first created     @override     public void oncreate(bundle savedinstancestate)     {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);     }      public static long addcontact(string name, string email)     {         if (db == null) {             db = new dbadapter(this); // <--- compiler error here         }          db.open();         long id = db.insertcontact("joe bloggs", "joe@bloggs.com");         db.close();         return id;     } } 

in addcontact method, on line: db = new dbadapter(this);, following error: cannot use 'this' in static context.

i familiar oop concepts understand why getting error - being new java itself, looking alternate methods on i'm trying achieve. dbadapter class constructor takes in context parameter, unsure why have not written class myself.

to clarify: understand why error occurring. dbadapter class constructor takes in context parameter, , don't know pass in context parameter when i'm using statically. want class static don't want have instantiate every time want use it.

i guess real question "why sqliteopenhelper require context?"

you have defined static method here :

  public static long addcontact(string name, string email) 

static methods , class variables tied class , not specific instance of class. cannot use this keyword inside refers current instance. 1 of choice declare method instance method removing static keyword method declaration, if indeed method logic depends on state of current instance.

i believe problem in using this inside static method during runtime if code calls static method classname.staticmethodname() , runtime have no idea how resolve this in context.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -