0

I want to store constructors or references to constructors in an SQL table to look up a class name and be able to access a constructor and instantiate from there. In C I would just store a pointer to a static function; how do I do it in Java? Any ideas?

// Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
                + KEY_CONSTRUCTOR + " ///SOMETHING THAT CAN DESCRIBE A CONSTRUCTOR///" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }
zmarties
  • 4,809
  • 22
  • 39
sqlbuddy
  • 79
  • 1
  • 10
  • you mean you want to store an object in DB? – Shubhang Malviya Jan 12 '15 at 13:46
  • You can't achieve that in Java, and it sounds like a horrible idea as well. – Kayaman Jan 12 '15 at 13:49
  • I want to associate a string to a constructor using an SQL table. Is that doable by storing an object? are there other ways? – sqlbuddy Jan 12 '15 at 13:55
  • 1
    possible duplicate of [Creating an instance using the class name and calling constructor](http://stackoverflow.com/questions/6094575/creating-an-instance-using-the-class-name-and-calling-constructor) – Joe Jan 12 '15 at 13:55
  • @Joe In the case of that question; would I be storing a string containing the name of the class in my table? – sqlbuddy Jan 12 '15 at 14:09
  • "In C I would just store a pointer to a static function" What makes you think the pointer would be useful for anything when read back? Practically all operating systems randomize address space layout. But for Java, have a look at `Class` and `Constructor` in `java.lang`. – laalto Jan 12 '15 at 15:16
  • If you just want to save something use Constructor.hashCode(), like the pointer easy to save, completely useless to reload. You could use Constructor.toString if instanciating a constructor is what you want. A bit tricky to parse, but possible. But if you want to store and reload an Object have a look on the serialization feature of java. – Bernd Ebertz Jan 12 '15 at 16:36

0 Answers0