Variables are created at different times and can be accessed by different things.

You need to know this!

public class myClass {

	/***** Global variables *****/
	/* these variables can be accessed anywhere in this class */
	//static variables (global)
	static int a = 100;
	static int b = (int) (0.75 * a);
	static final boolean YES = true;
	static final boolean NO = false;

	//instance variables (global)
	int c = 20;
	int d = c + 2;
	int speed = 3;

	static double squareMe(double d) {		//d is a local variable created here

		int e = 7;				//e is a local variable
		return d*d - e; 			//d is 5, the value that is passed in to squareMe
	}						//local variables d  and e are destroyed

	public static void main(String[] args) {
		System.out.print( "Global variable 'd' is " + d ); //d is 22 This is the global variable d
		System.out.print( squareMe(5) );
		int sum = 0;				//sum is created. It is a local variable
		for (int i = 0; i < 10; i++) {		//i is created. It is a local variable
			sum += i;			//you can use i here
		}					//i is destroyed and cannot be used anymore
		System.out.print( sum );
	}  //end of main				//all local variables created inside this { }, ie. args, sum, are destroyed
} //private global variables cannot be accessed outside of this class 


  1. Static variables and methods are created when the Java program is compiled (or run).
  2. Instance variables are made when an object is created with the word new.
    This means that each object has its own copies of the instance variables. So for example, if you have a number of balls, you can use one Ball class, but each ball can have a different position and speed.
  3. Local variables are created inside methods or code blocks. This happens when these code blocks are executed.
    They are destroyed when the code block ends at }
  4. If you have a local variable with the same name as a global variable, the local variable SHADOWS the global one.
    You can see this in the variable 'd'