Recently I wrote a class like this:
public class DBManager { private static MySqliteHelper helper; public static MySqliteHelper getHelper(Context context){ if (helper == null){ helper = new MySqliteHelper(context); } return helper; } public static void execSql(SQLiteDatabase db,String sql){ if (db != null){ if (sql != null && !"".equals(sql)){ db.execSQL(sql); } } } }
private static MySqliteHelper helper;
At the beginning, I did not add static modification to the above line of code, and then the tool reported an error.
Let's analyze the reasons carefully: static variables and static methods are shared by the entire class, and can be called regardless of whether the class has instantiated an object or not.
A reference to a non-static variable must first initialize the object.
"".equals(sql)
In addition, the above line of code is written in this way to avoid null pointer exception.