1 / 15

Static Variables

Static Variables. Normally, in a class, each variable belongs to its object. That is, each object has a different value for the variable. For example, the penguin1 may have color pink, while penguin2 may have color blue.

ahutchison
Download Presentation

Static Variables

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 Variables Normally, in a class, each variable belongs to its object. That is, each object has a different value for the variable. For example, the penguin1 may have color pink, while penguin2 may have color blue. Sometimes, we want a single variable to belong to the entire class, that all objects share the same value. If one object changes it, it is changed for all objects.

  2. Example For example, suppose that all snowmen live at the North Pole. We could have a field location which is set to the value “North Pole” that is shared by all the snowmen objects. If one snowman moves, say to “Southern California,” and changes the value of that field, then all the snowmen have moved to Southern California.

  3. Syntax To declare a variable so that it belongs to the entire class, rather than to each object of the class, use the keyword, static: private static String location = "North Pole";

  4. Counting Objects Here's a neat trick. Suppose we wanted to count the number of snowmen that were ever created. We could have a counter which is a class (static) variable. Every time a constructor is called, that counter is updated: private static int count = 0; public Snowman() { count++; } public int getCount() { return count; }

  5. Counting Objects (cont'd) Since the variable is shared, whenever the constructor is called, the common variable is updated. If count wasn't declared static, it would have a different value for each snowman. The value would always be 1.

  6. Static Methods Classes can also have static methods. These methods don't belong to any object, but rather the whole class. Since they aren't associated with an object, that cannot use any fields of an object. They can use static variables, however, which belong to the whole class. For example, in our Rational class, the gcd method could be a static method. It takes to ints and returns the greatest common denominator.

  7. Syntax Since a static method is not called upon an object, the syntax for invoking one is different. Instead of: <object>.<method>() the syntax for calling it (outside of the class) is: <class>.<method>()

  8. Example If the gcd method was declared static (and public), it could be called by: int g = Rational.gcd(n,m);

  9. More on Static Methods Static methods may only reference static variables and other static methods, since there is no object on hand. However, a static method could create an object and then use object methods on that object (just like any other program). The main method is always static.

  10. Examples The Math class declares two static variables (constants, in fact), Math.PI, and Math.E, and a number of useful static methods, e.g., Math.max(int,int), Math.max(double, double), Math.pow(double, double), Math.round(double), Math.ceil(double), and Math.floor(double).

  11. Wrapper Classes Often, we need to handle the primitive types (e.g., int, char, double, boolean), as full-blown objects. Sometimes a method expects an object rather than a primitive type. Certain storage methods work on objects, etc. For this reason, Java has wrapper classes, one per primitive type, from which one can create an object which which corresponds to a value of a primitive type.

  12. Integer For example, Integer is the wrapper class for ints. You normally create an Integer by using the one-argument constructor and pass in the int value, e.g., Integer age = new Integer(24); age is an object, and can be used anywhere an object is necessary. The Integer class also has a number of methods that are useful for integers.

  13. Unboxing Using the one-place Integer constructor converts an int to an Integer. The intValue() method converts an Integer to an int, i.e., gets back the value that corresponds to the Integer. For example, age.intValue() would be the int 32. This is called unboxing. Unboxing is done automatically when a type conversion is needed: int a = age;

  14. Integer Constants and Methods The Integer class also contains useful constants and methods related to integers: The constants include MAX_VALUE, MIN_VALUE, and SIZE. The methods include: parseInt(String) which converts a string to an Integer.

  15. Other Wrappers Other wrapper classes include Double, Boolean, and Character. Character, in particular, contains a number of useful methods dealing with characters.

More Related