在SQLiteQueryBuilder类上应用内连接时

时间:2016-04-09 12:38:14

标签: android sqlite android-sqlite

为什么以下代码放在静态块中?

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);
}

1 个答案:

答案 0 :(得分:0)

这是static block,在Java中被视为static constructor。它基本上用于你想要做一次的课程。

例如,初始化static变量sWeatherByLocationSettingQueryBuilder。如果没有这个静态块,您应该从类外部初始化静态变量,这通常是不合适的。

有关详细信息,您还可以看到以下帖子:Java - Can final variables be initialized in static initialization block?