为什么以下代码放在静态块中?
private static final SQLiteQueryBuilder sWeatherByLocationSettingQueryBuilder;
//static block:
static{
sWeatherByLocationSettingQueryBuilder = new SQLiteQueryBuilder();
//This is an inner join which looks like
//weather INNER JOIN location ON weather.location_id = location._id
sWeatherByLocationSettingQueryBuilder.setTables(
WeatherContract.WeatherEntry.TABLE_NAME + " INNER JOIN " +
WeatherContract.LocationEntry.TABLE_NAME +
" ON " + WeatherContract.WeatherEntry.TABLE_NAME +
"." + WeatherContract.WeatherEntry.COLUMN_LOC_KEY +
" = " + WeatherContract.LocationEntry.TABLE_NAME +
"." + WeatherContract.LocationEntry._ID);
}
答案 0 :(得分:0)
这是static block
,在Java中被视为static constructor
。它基本上用于你想要做一次的课程。
例如,初始化static
变量sWeatherByLocationSettingQueryBuilder
。如果没有这个静态块,您应该从类外部初始化静态变量,这通常是不合适的。
有关详细信息,您还可以看到以下帖子:Java - Can final variables be initialized in static initialization block?