title: Using Global Variables
body: <p>I know this question has been asked a million times because I have done some research and have found many threads on this.  I have tried to use the answers in those threads but I am having a bit of trouble.</p>
  
  <p>I am looking to set a few variables that I can use across all of my activities.</p>
  
  <p>I created a GlobalVariables.java class which looks like the following (the value in there is just for testing purposes as of now):</p>
  
  <pre><code>import android.app.Application;
  
  public class GlobalVariables extends Application {
  
  int holeAmount;
  
  public int getHoles(){
      return holeAmount;
    }
    public void setHoles(String s){
      holeAmount = 30;
    }
  
  }
  </code></pre>
  
  <p>in my main activity where everything is happening I have the following:</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
      GlobalVariables global = ((GlobalVariables)getApplicationContext());
      int totalHoles = global.getHoles();
  </code></pre>
  
  <p>and on the "GlobalVariables global = ..." line I am getting the following error:</p>
  
  <pre><code>Multiple markers at this line
  - GlobalVariables cannot be resolved 
   to a type
  - GlobalVariables cannot be resolved 
   to a type
  </code></pre>
  
  <p>I tried to follow the instructions here but clearly I am doing something incorrectly.  > <a href="https://stackoverflow.com/questions/708012/android-how-to-declare-global-variables">How to declare global variables in Android?</a></p>
  
  <p>Any help would be greatly appreciated!</p>
  
  <p>Thanks!</p>
  
  <hr>
  
  <hr>
  
  <p><strong>SECOND ATTEMPT:</strong></p>
  
  <p><strong>EasyPar.java</strong> (Errors @ EasyParHelperActivity)</p>
  
  <pre><code>package com.movi.easypar;
  
  import java.text.DateFormat;
  import java.util.Date;
  
  import android.app.Activity;
  import android.app.AlertDialog;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.database.Cursor;
  import android.graphics.PixelFormat;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.view.Window;
  import android.widget.Button;
  import android.widget.ScrollView;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class EasyPar extends Activity implements OnClickListener {
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
  (initialize all my buttons and textviews)
  }
  
  public void someMethod() {
      EasyParHelperActivity.helper.setHoles(30);
      int holes = EasyParHelperActivity.helper.getHoles();
  }
  
  (the rest of my button functions, etc.)
  </code></pre>
  
  <p><strong>EasyParHelperActivity</strong> (No Errors)</p>
  
  <pre><code>package com.movi.easypar;
  
  import android.app.Application;
  
  public class EasyParHelperActivity extends Application {
  
  public static EasyParHelper helper = new EasyParHelper();
  
  }
  </code></pre>
  
  <p><strong>EasyParHelper.java</strong> (No Errors)</p>
  
  <pre><code>package com.movi.easypar;
  
  public class EasyParHelper {
  
  private int holeAmount = 0;
  
  public int getHoles() {
      return holeAmount;
  }
  
  public void setHoles(int holes) {
      holeAmount = holes;
  }
  }
  </code></pre>
  
  <p>All i want is for the user to be able to click a button "18" or "9" on the first screen, and for the application to be able to use that value other times throughout whatever it does.  So I need to set it in screen 1, and in screen 2 i need to retrieve that value.</p>