title: Correct way to get the instance of Application in android
body: <p>Which of these ways is more proper for getting the instance of Application</p>
  
  <ol>
  <li><p>Initialise static field in Application.onCreate() and provide static access to it</p>
  
  <pre><code>public class MyApplication extends Application {
  
      private static MyApplication sInstance;
  
      @Override
      public void onCreate() {
          super.onCreate();
          sInstance = this;
      }
  
      public static MyApplication getInstance() {
          return MyApplication.sInstance;
      }
  }
  
  public class MyBroadcastReceiver extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
          MyApplication application = MyApplication.getInstance();
      }
  }
  </code></pre></li>
  <li><p>Create static method which takes Context as param and cast that Context to MyApplication</p>
  
  <pre><code>public class MyApplication extends Application {
  
      @Override
      public void onCreate() {
          super.onCreate();
      }
  
      public static MyApplication getInstance(Context context) {
          return ((MyApplication) context.getApplicationContext());
      }
  }
  
  public class MyBroadcastReceiver extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
          MyApplication application = MyApplication.getInstance(context);
      }
  } 
  </code></pre></li>
  </ol>