
class Feb17 {

    // variables defined outside a method (but within the class) are called fields.
    // the static keyword specifies that all instances of the class share the same
        // instance of the field.

	static int x = 10;  // field

	public static void main(String[] args) {
		x = 10;
		System.out.println(x);

		x = 7;
		System.out.println(x);

		foo();  // foo has access to the field as well
		System.out.println(x);

		Player p1 = new Player();
		p1.score = 10;

		Player p2 = new Player();

        // Static fields are shared by all instances, so a change to the
        // field by one instance will affect all instances
		System.out.println("p1: " + p1.score);
		System.out.println("p2: " + p2.score);

	    // Static fields and methods can be access using the name of the class,
	    // since all instances share the field or method
		System.out.println(Player.score);

        // All Math class methods are static so are accessed using the name of the class (Math)
		System.out.println(Math.sqrt(25));

        // Non-static fields are methods must be called on an instance of the class.
		p1.foo(p1);
		p1.foo(p2);

        // health is non-static so must be accessed using an instance
		System.out.println(p1.health);
	}

	static void foo() {
		System.out.println("inside foo: " + x);
	}
}
