190 likes | 328 Views
Static and Global. The Plan For Today. Static Methods – occasionally a good idea Why Globals Are Bad Static Variables – usually a bad idea static final stuff static in inner classes static initialization. The Idea of Classes. Combine data and the functions that operate on that data
E N D
The Plan For Today • Static Methods – occasionally a good idea • Why Globals Are Bad • Static Variables – usually a bad idea • static final stuff • static in inner classes • static initialization
The Idea of Classes • Combine data and the functions that operate on that data • BUT occasionally you have a function that doesn’t really need any data beyond its parameters: class MathObject { public double computeFactorial(int n) {…} } // elsewhere MathObjectfoo = new MathObject(); result = foo.computeFactorial(75);
Static! • “Static” in Java is something that refers to an object’s class, rather than a particular instance of the class • You can call a static function on a class, rather than a particular object class MathObject { public static double computeFactorial(int n) {…} } // elsewhere // I have never created a new MathObject! result = MathObject.computeFactorial(75);
Static! • In a static method, you can’t refer to instance variables of the class. Which makes sense. Which instance would you be referring to? class Point { private double x, y; public static double getX() { //does not make sense return x; } //other code } // elsewhere Point a = new Point(1,2); Point b = new Point(2,3); Point.getX(); //huh?
Static methods are usually used for handy utility methods • Sometimes they’re in a class with non-static stuff too, if utility method makes sense with a particular class • Sometimes you’ll make a whole class just filled with utility methods double cosineOfAngle = Math.cos(angle);
The Plan For Today • Static Methods – occasionally a good idea • Why Globals Are Bad/Static Variables • static final stuff • static in inner classes • static initialization
Static! • “Static” in Java is something that refers to an object’s class, rather than a particular instance of the class • There is only one static variable instance for an class (meaning that its value is shared by all instances of the class) //a deeply flawed point class class Point { private static double x, y; public Point(double inputX, double inputY) { x = inputX; y = inputY; } public static double getX() { return x; } //other code } // this code now compiles Point a = new Point(1,2); Point b = new Point(2,3); Point.getX(); //what does this return
Just to Practice //a deeply flawed point class class Point { private static double x, y; public Point(double inputX, double inputY) { x = inputX; y = inputY; } public static double getX() { return x; } //other code } // this code now compiles Point a = new Point(1,2); Point b = new Point(2,3); Point.getX(); //what does this return • Make a new class • Give it a mix of static and non-static varaibles • Give it at least 1 static method that accesses the static variables • Verify yourself that although different instances of the class can have different non-static variables, changing the static variable in one instance changes it for every instance
Why Globals Are Bad class Foo { public static intglobalVal = 7; } //anywhere intcomputedVal = Foo.globalVal + 99; Foo.globalVal = 22; • Non-locality • Concurrency Issues
It Should Be Clear What State Code Depends on myVariable = 77; otherObject.doFunction(); myVariable = 6;
Why Globals Are Bad class Foo { public static intglobalVal = 7; } //anywhere intcomputedVal = Foo.globalVal + 99; Foo.globalVal = 22; • Non-locality • Any part of the code can depend on them • It’s difficult to know if some code will use or modify their values • Code should make clear what values they depend on and what values they modify. • Concurrency Issues
Is it really the case there will only ever be one? • Example: Picassa Model • Think about testing
Why Globals Are Bad class Foo { public static intglobalVal = 7; } //anywhere intcomputedVal = Foo.globalVal + 99; Foo.globalVal = 22; • Non-locality • Any part of the code can depend on them • It’s difficult to know if some code will use or modify their values • Code should make clear what values they depend on and what values they modify. • Concurrency Issues • You shouldn’t assume there’s only going to be value for something everywhere in your code unless you have to • At the minimum, use the Singleton pattern. It doesn’t fix the problems of globals but it at least protects their values and makes it easy to refactor away from globals if you need to
Singleton • Object that has only one instance //always returns same object SingletonObjectfoo = SingletonObject.getInstance(); • Functions protect global data (but still act global) • You can minimize dependence on globals by making the SingletonObject a parameter to many functions rather than making them use it as a global public doSomething(Parameter a, StingletonObjectglobalState);
Constants in Java class SomeClass { public static final double FEIGENBAUM_CONSTANT = 4.669201609102; } Because its value will never change, this doesn’t really have many of the same problems. Feel free to use constants if you need them.
Static in Inner Classes See the code I handed out
Static block class ClassWithSomeStaticStuff { static { //runs when the class is loaded //usually used to initialize static variables } }
Final Challenge • Write a class that keeps all its instances in a list – that is everytime an instance of class is created, it’s added to this list • Make a static method to return this list • How to do this: • Make a static variable that contains the list • In a static block initialize it (to empty) • In the objects constructor, add the object to the list (use this) • Make a static method that returns that static variable