Static Class Members Wrapper Classes Autoboxing Unboxing

advertisement
Static Class Members
Wrapper Classes
Autoboxing
Unboxing
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.
Static Class Members
• Determining if a method or variable should
be static is an important design decision
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.
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.
5
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
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);
7
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
8
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
Wrapper Classes
• The java.lang package contains a wrapper
class that corresponds to each primitive type:
Primitive Type
byte
short
Wrapper Class
Byte
Short
int
long
float
Integer
Long
Float
double
char
boolean
void
Double
Character
Boolean
Void
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
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
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
Unboxing
• The reverse conversion (called unboxing)
also occurs automatically as needed
Download