0

Why below code is placed in a static block?

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);
}
Pooya
  • 6,083
  • 3
  • 23
  • 43

1 Answers1

0

This is static block which is treated as static constructor in Java. It is basically used in classes where you want to do something once.

For example initializing static variable sWeatherByLocationSettingQueryBuilder. Without this static block you should initialize your static variables from outside of the class which usually is not suitable.

For more info you can also see this post: Java - Can final variables be initialized in static initialization block?

Community
  • 1
  • 1
Pooya
  • 6,083
  • 3
  • 23
  • 43