- Forward


Developing Classes that have Static Members
With Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review
Back SMYC Forward
The Process Thus Far
  1. Read the textual description
  2. Create an initial encapsulation with private attributes
  3. Add a constructor
  4. Add a toString() method
  5. Add an equals() and/or compareTo() method
  6. Add necessary accessors
  7. Add necessary mutators
  8. Add useful private methods (i.e., helper methods)
  9. Add convenient overloaded methods
  10. Add convenient overloaded constructors
Motivation
Back SMYC Forward
  • One Observation:
    • Sometimes a class has attributes and/or all of the objects in a class have the same value for one or more attributes
  • Another Observation:
    • Sometimes an object's behavior does not depend on its attributes (in which case, it is as if the behavior belongs to the class)
Class Attributes
Back SMYC Forward
  • Definition:
    • Attributes that belong to a class (or all of its instances) rather than individual instances of a class
  • Implication:
    • They can be used without instantiating an object
  • Java:
    • Must be declared static
Class Attributes (cont.)
Back SMYC Forward
  • Common Uses:
    • Class constants, flags and special values
    • Common values (across all instances)
    • Object counters
  • An Observation:
    • These situations may arise both in classes you develop and classes you use
Class Attributes - Constants etc...
Back SMYC Forward
  • In the Integer Class:
    • MIN_VALUE
    • MAX_VALUE
  • In the Double Class:
    • NEGATIVE_INFINITY
    • POSITIVE_INFINITY
    • MIN_VALUE
    • MAX_VALUE
  • In the Math Class:
    • E (base of the natural log)
    • PI
Class Attributes - Constants etc... (cont.)
Back SMYC Forward

An Improved PictureFrame Class

javaexamples/oopbasics/pictureframe/staticattributes/PictureFrame.java
 
Class Attributes - Constants etc... (cont.)
Back SMYC Forward

Building a Model of Memory

PictureFrame p, q; p = new PictureFrame(8.0, 10.0, 0.0, false); q = new PictureFrame(3.0, 5.0, 0.0, false); System.out.printf("Cost: %6.2f\n", p.getCost());
Class Attributes - Constants etc... (cont.)
Back SMYC Forward
  • Could We Use Instance Attributes?
    • Yes
  • How Can We Know?
    • Build a model of memory and trace a method call assuming they aren't static
  • Why Shouldn't We?
    • It is confusing to do so because non-static attributes should be defining characteristics of instances of the class
    • It wastes memory (though this is of secondary importance)
Class Attributes - Common Values
Back SMYC Forward

An Example

public class ProgressiveSlotMachine { // Attributes of the class private static double jackpot; // Attributes of objects private double currentBet; . . . private void handleLosingSpin() { jackpot = jackpot + 0.5 * currentBet; } }
Class Attributes - Object Counters
Back SMYC Forward

An Example

/** * A user account */ public class UserAccount { // Attributes of the class private static int nextAccountNumber; // Attributes of objects private int accountNumber; private String name; /** * Explicit Value Constructor * * @param name The name of the account holder */ public UserAccount(String name) { // Store the name this.name = name; // Store the account number accountNumber = nextAccountNumber; // Setuup the account number for the next account nextAccountNumber++; }
Class Attributes - Object Counters (cont.)
Back SMYC Forward

Building a Model of Memory

UserAccount u, w; u = new UserAccount("Fred"); w = new UserAccount("Wilma");
Class Attributes - Common Attributes and Counters (cont.)
Back SMYC Forward
  • Could We Use Instance Attributes?
    • No
  • Why Not?
    • Each object would have its own value (which is exactly what we are trying to avoid)
  • How Can We Know?
    • Build a model of memory and trace a method call assuming they aren't static
Class Behaviors
Back SMYC Forward
  • Definition:
    • Methods that belong to a class rather than instances of a class
  • Implication:
    • They can be used without instantiating an object
  • Java:
    • Must be declared static
Class Behaviors (cont.)
Back SMYC Forward
  • Common Uses:
    • Type conversion
    • "Factory" methods
    • Attribute-free calculations
  • An Observation:
    • These situations may arise both in classes you develop and classes you use
Class Behaviors - Type Conversion
Back SMYC Forward
  • In the Integer Class:
    • parseInt()
    • toString()
  • In the Double Class:
    • parseDouble()
    • toString()
Class Behaviors - Attribute-Free Methods
Back SMYC Forward
  • In the Math Class:
    • abs()
    • max() and min()
  • In the Color Class:
    • RGBtoHSB()
Class Attributes - Attribute-Free Methods (cont.)
Back SMYC Forward
  • Could We Use Instance Methods?
    • Yes
  • Why Shouldn't We?
    • It is confusing to do so because non-static methods should be behaviors that define instances of the class
    • It is inconvenient (e.g., Why should we be required to construct an instance of a Math object to calculate the square root of a number?)
Class Behaviors - "Factory" Methods
Back SMYC Forward
  • Definition:
    • Methods that are used like constructors
  • Observations:
    • Sometimes construct the object when asked, sometimes construct a "pool" and just return one when asked
    • Sometimes have obvious names (like getInstance()) and sometimes do not
Class Behaviors - "Factory" Methods (cont.)
Back SMYC Forward
  • In the String Class:
    • format()
    • valueOf()
  • In the NumberFormat Class:
    • getInstance()
  • In the Color Class:
    • getColor()
Class Attributes - "Factory" Methods (cont.)
Back SMYC Forward
  • Could We Use Instance Methods?
    • No
  • Why Not?
    • We would have to have an object in order to create one
A Common Mistake
Back SMYC Forward
  • Three Related Questions:
    • What happens if you use a non-static attribute/method in a static method, why, and when?
    • public class Interval { private double left, right; public Interval(double l, double r) { left = l; right = r; } // What's happens here, why, and when? public static double getLength() { return right - left; } }
  • Another Question:
    • How do you fix this problem? (Hint: The answer is not to behave like 10,000 monkeys at 10,000 keyboards!)
A Common Mistake (cont.)
Back SMYC Forward
  • Where Else Beginning Programmers Often Encounter This:
    • In the main() method of the main class
    • public class Game { private int lives; // What's happens here and why? (And when?) public static void main(String[] args) { lives = Integer.parseInt(args[0]); } }
  • Another Question:
    • How do you fix this problem? (Hint: Again, the answer is not to behave like 10,000 monkeys at 10,000 keyboards!)
A Common Confusion
Back SMYC Forward
  • One Question:
    • Can I use a static attribute/method as if it is non-static?
  • Another Question:
    • Should I?
The Process Thus Far
Back SMYC Forward
  1. Read the textual description
  2. Create an initial encapsulation with private attributes
  3. Add a constructor
  4. Add a toString() method
  5. Add an equals() and/or compareTo() method
  6. Add necessary accessors
  7. Add necessary mutators
  8. Add useful private methods (i.e., helper methods)
  9. Add convenient overloaded methods
  10. Add convenient overloaded constructors
  11. Add necessary static attributes
  12. Add necessary static methods
There's Always More to Learn
Back -