title: How to pass (Android) application context to a Java class?
body: <p>I have the following code in which I am using the application context to retrieve needed information:</p>
  
  <pre><code>public class Data{
     private boolean VarA;
  
     public void setVarA(boolean B,Context ctx)
     {
          SharedPreferences CoreDataStorage = ctx.getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = CoreDataStorage.edit();
          editor.putBoolean("PrefVarA", VarA);
          edit.commit();
     }
  
  }
  </code></pre>
  
  <p>Now I am calling the public method setVarA() from the below class</p>
  
  <pre><code>public class MyActivity extends Activity{
  
      Data cd = new Data();
  
      @Override
          public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.registration);
          cd.setVarA(true,this);
      }
  }
  </code></pre>
  
  <p>In the activity above it shows me compilation error that it can't cast from MyActivity to Context. Please suggest any solution. Is the above code is not proper way to pass the context?</p>