Presentation is loading. Please wait.

Presentation is loading. Please wait.

Static Class Members Wrapper Classes Autoboxing Unboxing.

Similar presentations


Presentation on theme: "Static Class Members Wrapper Classes Autoboxing Unboxing."— Presentation transcript:

1 Static Class Members Wrapper Classes Autoboxing Unboxing

2 Static Class Members Recall that a static method is one that can be invoked through its class name For example, the methods of the Math class are static: result = Math.sqrt(25) Variables can be static as well.

3 Static Class Members Determining if a method or variable should be static is an important design decision

4 The static Modifier We declare static methods and variables using the static modifier It associates the method or variable with the class rather than with an object of that class Static methods are sometimes called class methods and static variables are sometimes called class variables.

5 5 Static Variables Normally, each object has its own data space, but if a variable is declared as static, only one copy of the variable exists private static float price; Memory space for a static variable is created when the class is first referenced.

6 Static Variables All objects instantiated from the class share its static variables Changing the value of a static variable in one object changes it for all others

7 7 Static Methods class Helper { public static int cube (int num) { return num * num * num; } Because it is declared as static, the method can be invoked as value = Helper.cube(5);

8 8 Static Class Members The order of the modifiers can be interchanged, but by convention visibility modifiers come first Recall that the main method is static – it is invoked by the Java interpreter without creating an object

9 Static Class Members Static methods cannot reference instance variables because instance variables don't exist until an object exists However, a static method can reference static variables or local variables

10 Wrapper Classes The java.lang package contains a wrapper class that corresponds to each primitive type: Primitive TypeWrapper Class byteByte shortShort intInteger longLong floatFloat doubleDouble charCharacter booleanBoolean voidVoid

11 Wrapper Classes The following declaration creates an Integer object which is a reference to an object with the integer value 40 Integer age = new Integer(40); An object of a wrapper class is used in situations where a primitive value will not suffice For example, some objects serve as containers of other objects Primitive values could not be stored in such containers, but wrapper objects could be

12 Wrapper Classes Wrapper classes may contain static methods that help manage the associated type –For example, the Integer class contains a method to convert an integer stored in a String to an int value: num = Integer.parseInt(str); Wrapper classes often contain useful constants –For example, the Integer class contains MIN _ VALUE and MAX _ VALUE for the smallest and largest int values

13 Autoboxing Autoboxing is the automatic conversion of a primitive value to a corresponding wrapper object: Integer obj; int num = 42; obj = int; The assignment creates the appropriate Integer object

14 Unboxing The reverse conversion (called unboxing) also occurs automatically as needed


Download ppt "Static Class Members Wrapper Classes Autoboxing Unboxing."

Similar presentations


Ads by Google