1

I've read the questions/answers saying that Java doesn't have nor is it likely to get C# style properties. However, in this question: How to organize database access code in Android project?, there is an answer with some sample code like this:

c = localDBRW.rawQuery(sql, selectionArgs);

In the comments to that answer he says that localDBRW is "defined as a reference to getWritableDatabase()." To me that means that in the line above getWritableDatabase() is called and rawQuery is called on the result of getWritableDatabase().

So my question is: How is that done?

In C# it would be trivial. I'm new to Java, so I'm not even sure what the terminology is that would lead me to an answer. Searches on "properties", "methods", and "references" haven't led to this particular usage.

Summary of answers:

I was grasping at a dream. I was interpreting the original statement in a way that would mean my wishes could come true. Alas, that is not the way of things. Java is without any hope of C# property syntax and I am banished to an eternity of writing obj.getX() instead of obj.X.

Community
  • 1
  • 1
Jere.Jones
  • 9,915
  • 5
  • 35
  • 38
  • *"I am banished to an eternity of writing obj.getX() instead of obj.X"* - Its all right son, you'll get over it. – Stephen C Jun 30 '10 at 13:41

3 Answers3

1

localDBRW is "defined as a reference to getWritableDatabase()"

I believe this simply means that there was this statement prior to the usage, and nothing more:

localDBRW = getWritableDatabase();

There is currently no property-like semantics in Java. You can't, using a field access syntax, invoke a method instead. The closest thing to this would be if a field is accessed for the first time and that triggers its initialization code, but that's an entirely different construct.

Related questions

Community
  • 1
  • 1
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
  • 1
    Greetings. The code in question was written by me. You are correct - `localDBRW` was defined earlier: `SQLiteDatabase localDBRW;`. Then it is set to the value of `getWritableDatabase()`. In Android `getWritableDatabase()` and `getReadableDatabase()` just return the current read/write database object, and if it's not defined, they open it first and make sure it's defined. – Brad Hein Jun 30 '10 at 12:28
1

localDBRW is a getWritableDatabase(). It is defined as a reference to getWritableDatabase(). Instead of localDBRW I recommend just calling getWritableDatabase() when you need it.

Do not take it literally. localDBRW is an attribute and initialized (somewhere) with the result of a getWritableDatabase() call. So it is a writable database. In Java an attribute can never hold a reference to a method (function pointer).

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
1

Edited in response to your clarification. I've kept the old answer in case it's useful to someone.

Java has reflection APIs to allow you to find and invoke arbitrary named methods of an object. So you would not have a single

 theMethod.invoke()

call but rather

 // assuming you have a Class clazz and Object clazzObj
 Method theMethod= clazz .getMethod(
          "someName", null /* or a list of desired params*/);

 theMethod.invoke(clazzObj, arglist);

I think with this (perhaps with a suitable class wrpaping the { clazzObj, theMethod } pair you can get very close to the behaviour you need, which I guess is some kind of data-drived dispatch.

=== old answer ====

So far as I can see you are tryint to execute some SQL with some arguments and then work with the rows and columns that result.

So that can be done with a JDBC prepared statement call, with some iteration over the result set. Result sets permit you to access columns by number.

I don't see any difference between the Java and C# approaches here. The example you reference has

c.moveToFirst();
recordID = c.getString(c.getColumnIndex("id"));
c.close();

A JDBC tutorial shows code like this

String query = "
SELECT COFFEES.COF_NAME " +
   "FROM COFFEES, SUPPLIERS " +
   "WHERE SUPPLIERS.SUP_NAME LIKE 'Acme, Inc.' " +
   "and SUPPLIERS.SUP_ID = COFFEES.SUP_ID";

ResultSet rs = stmt.executeQuery(query);
System.out.println("Coffees bought from Acme, Inc.: ");
while (rs.next()) {
    String coffeeName = rs.getString("COF_NAME");
    System.out.println("     " + coffeeName);
}

where you are obtaining coumns by name.

djna
  • 54,992
  • 14
  • 74
  • 117
  • The sql/database is not really relevant to the question. I was asking about the running of a method without using (). But thanks! – Jere.Jones Jun 30 '10 at 09:45