Nested Java Classes

Soldato
Joined
9 Jun 2006
Posts
2,642
Hi,

I've decided to do some development for Android, and I am currently working through the tutorials to familiarise myself with the Android framework.

While looking through the provided code, I was confused as to why an inner class was declared static. I wouldn't say I am a novice to programming as I do it for a living, and I understand the purpose of the static keyword when used with variables and methods, but I've never really had any experience with using it with a class definition.

From what I've read, you only add a static keyword to a nested inner class if it doesn't need to be instantiated or access instance data of the outer class. This doesn't match what is in the example below as the open() method of the outer class instantiates the static class.

So I'm either missing something or the writer has done it in 'error'.

Would appreciate if someone could enlighten me as to why and when you would want a nested static class over a non-static one :)

Below is the cut version of the tutorial code:

Code:
public class NotesDbAdapter {

    private DatabaseHelper mDbHelper;

    /**
     * Database creation sql statement
     */
    private static final String DATABASE_CREATE =
        "create table notes (_id integer primary key autoincrement, "
        + "title text not null, body text not null);";

    private static final String DATABASE_NAME = "data";
    private static final String DATABASE_TABLE = "notes";
    private static final int DATABASE_VERSION = 2;

    private final Context mCtx;

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS notes");
            onCreate(db);
        }
    }

    /**
     * Constructor - takes the context to allow the database to be
     * opened/created
     * 
     * @param ctx the Context within which to work
     */
    public NotesDbAdapter(Context ctx) {
        this.mCtx = ctx;
    }
    public NotesDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }
}
 
Last edited:
Static inner classes can have static members (because the class is not associated with a particular object). Non-static inner classes can access non-static fields in the outer class (because the class is associated with a particular object).

Neither of these differences apply to the inner class in this case so it doesn't matter whether it's defined as static or not.

Looks like an error, it even has a constructor.

? Static inner classes can have constructors.
 
I see now, it's declared ala "this static property is a class" rather than "this internal class is static". I'm of the opinion that nested class smell like unwashed socks anyway tbh :)
 
Back
Top Bottom