1 / 19

Static and Global

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

hamlin
Download Presentation

Static and Global

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Static and Global

  2. 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

  3. 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);

  4. 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);

  5. 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?

  6. 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);

  7. 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

  8. 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

  9. 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

  10. Why Globals Are Bad class Foo { public static intglobalVal = 7; } //anywhere intcomputedVal = Foo.globalVal + 99; Foo.globalVal = 22; • Non-locality • Concurrency Issues

  11. It Should Be Clear What State Code Depends on myVariable = 77; otherObject.doFunction(); myVariable = 6;

  12. 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

  13. Is it really the case there will only ever be one? • Example: Picassa Model • Think about testing

  14. 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

  15. 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);

  16. 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.

  17. Static in Inner Classes See the code I handed out

  18. Static block class ClassWithSomeStaticStuff { static { //runs when the class is loaded //usually used to initialize static variables } }

  19. 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

More Related