20.5 Problem 1:

class CUStudent {
    public static String location;
}

public class TestStatic {
    public static void main(String[] args) {
        // All CUStudents will now live in Ithaca:
        CUStudent.location = "Ithaca";
    
        // Lets check a CUStudents location:
        CUStudent c1 = new CUStudent();
        System.out.println(c1.location);
        
        // Lets demonstrate the syntax and danger of static...
        // An object can access a static member if the member is
        // in the scope:
        CUStudent c2 = new CUStudent();
        c2.location = "Timbuktu";
        
        // Check what happened to all students:
        System.out.println(c1.location);
        System.out.println(CUStudent.location);
    }
}

/*  The above code will change location across all instances since location is a static field
 *  of class CUStudent.
 */

20.6 Problem 2:

/*  The main method is static in Java since it is called without creating an instance of the
 *  class to which it belongs.  It is used as a code entry point and more importantly, only called
 *  once in a normal program.  It wouldn't make sense to make the main method instance dependent.
 */

20.7 Problem 3:

    public class TestMain {
        public int x;
        public static void main(String[] args) {
            System.out.println(x);
        }
    }

 /*  
 *  The above code fails because x is not a static variable and hence cannot be called from a
 *  static method.  In other words, x belongs to an instance of class TestMain, but as stated
 *  above, the main method is called without creating and instance, and as a result, Java doesn't
 *  understand what is meant by x.
 */

20.8 Problem 4:

 class Student {
    private String name;
    private static int count;
    public static int currentYear;
    public static final int GRADYEAR = 2005;
    
    public Student(String name) {
        this.name=name;
        count++;
    }

    public static int getCount() { return count; }
 }
 
 public class StaticTest2 {
    public static void main(String[] args) {
        System.out.println(Student.GRADYEAR);
        Student s1 = new Student("Dani");
        Student s2 = new Student("Shagrath");
        Student.currentYear = 2001;
        System.out.println(s2.currentYear);
        System.out.println(Student.getCount());
    }
 }
 *
 *  The above code should output:
 *  2005
 *  2001
 *  2
 *
 *  since count is a static field incremented everytime the constructor for Student
 *  is called (it is called twice here).
 */
