I think I have already mentioned before that due to the Object Oriented Programming college class I have to play around Java now. I was assigned to do a project in Java and I decided to check what I can do there. Being more a perl guy, I have a faced a numerous problems with Java which I try to overcome in all possible ways. Here is what problems I have:
1. Static method declaration in abstract class. This really drives me crazy :(. I have a case where I really need to have static methods in abstract class (with implementation code inside) and I can not do it which causes me a lot of headache.
2. I am really missing an analog of perl keyword __PACKAGE__ in Java. This, sometimes also have to do with the first issue. For example I want to create an abstract class which defines the structure of a database table model, and this class should have static methods such as retrieveAll or search to return an array of objects of this class. Lets assume that I am allowed to have static methods in abstract classes and that I have a __PACKAGE__ option, then I would write something like this:
abstract public class DBModel extends DB {
abstract static private String dbTableName;
static public __PACKAGE__ retrieveAll () {
DB myDB = new DB();
myDB.query(\"SELECT id FROM \" + dbTableName + \"\");
if (myDB.getResultCount() > 0) {
int i = 0;
__PACKAGE__ itemList[];
while (myDB.getResultSet().next()) {
itemList[i++] = __PACKAGE__ new( myDB.getResultSet().getInt(\"id\") );
}
return itemList;
}
return null;
}
}
public class MCategories extends DBModel {
static private String dbTableName = \"customers\";
}
Like this, all classes which extend DBModel would have a static method retrieveAll() which would retrieve a list of all items from the DB.
3. Declaration and assignment to a variable inside of if/while/whatever. Sometimes I just need to do something like this (using the above example):
if ((Object[] objectsList= MCategories.retrieveAll()) != null) {
// do something with objectsList[].
}
If you know some workarounds, please let me know.