Title: Static Methods Wrapper Class
1Static MethodsWrapper Class
- Dr. Hairong Zhao
- hairong_at_calumet.purdue.edu
- http//ems.calumet.purdue.edu/mcss/zhao
- Purdue University Calumet
2Static Methods
- Static methods belong to a class and do not
require any object. - The method can be invoked using the class name
3Predefined Classes with Static Methods
- Math
- part of the java.lang package
- The Math class contains methods that perform
various mathematical functions - Math.pow(2.0, 3.0) Math.sqrt(6.0)
Math.PI Math.max(2.0, 3.0) - Random
- part of the java.util package
- provides methods that generate pseudorandom
numbers - System
- Character
4Static Methods in Class Character
5Write the class Circle1
- instance variables
- private double radius
- methods
- public void setRadius(double newRadius)
- public double getRadius()
- public static double area(double radius)
6Write the class Circle1
7Write the test class Circle1Demo
- public class Circle1Demo
-
- public static void main(String args)
-
- Circle1 myCircle new Circle1( )
- myCircle.setRadius(2)
- double myArea myCircle.area(myCircle.get
Radius()) - System.out.println("If my circle has
radius " myCircle.getRadius()
", then its area is " myArea) -
- System.out.println()
- System.out.println("Please choose a new
radius for me") -
- Scanner keyboard new Scanner(System.in)
- double newRadius keyboard.nextDouble(
) - myCircle.setRadius(newRadius)
- System.out.print("Now my circle has a new
radius " - newRadius)
- System.out.println(" and its area is "
Circle1.area(newRadius)) -
8Using an Object to Call a Static or non-static
Method
- An object of the class can be used to call a
static method of the class even though it is more
common to use the class name to call the static
method. - You cannot invoke a nonstatic method, or refer to
instance variables within a static method unless
you create and use a calling object for the
nonstatic method.
9Mixing Static and Nonstatic Methods
- public void showArea( )
-
- System.out.println("Area is "
this.area(radius)) -
- public static void areaDialog( )
-
- Scanner keyboard new Scanner(System.in)
- System.out.println("Enter the radius of a
circle") - double newRadius keyboard.nextDouble( )
- Circle2 c new Circle2( )
- c.setRadius(newRadius)
- c.showArea( )
10Test Methods showArea and areaDialog
11Putting main in Any Class
- A class which contains a method main serves two
purposes - It can be run as a program.
- It can be used to create objects for other
classes.
12Putting main in Any Class, cont.
- A programs main method must be static.
- A nonstatic method in the same class cannot be
invoked unless an object of the class is created
and used as a calling object for the nonstatic
method. - In general, dont provide a method main in a
class definition if the class will be used only
to create objects.
13Putting main in Circle Class
14Static Variables
- A class can have static variables and constants
as well as static methods. - public static final double PI 3.14159
- public static int numberOfInvocations 0
- The value of a static variable can be changed by
any method that can access the variable.
15Static Variables, cont.
- Like instance variables, static variables
generally are declared private. - They should be read only by accessor methods.
- They should be changed only by mutator methods.
- Every object of the class has access to the
static variable(s) via the (public) accessor and
mutator methods.
16Static Variables, cont.
- Static variables are also called class variables.
- The primary purpose of static variables (class
variables) is to store information that relates
to the class as a whole - The number of objects that have been created
- A constant that applies for all objects, PI,
interestRate
17public class StaticDemo private static int
numberOfInvocations 0 public static
void main(String args) int i
StaticDemo object1 new StaticDemo( )
for (i 1 i lt10 i)
object1.outPutCountOfInvocations( )
StaticDemo object2 new StaticDemo( )
for (i 1 i lt10 i)
object2.justADemoMethod( )
System.out.println("Total number of invocations
" numberSoFar( )) public
void justADemoMethod( )
numberOfInvocations //In a real
example, more code would go here.
public void outPutCountOfInvocations( )
numberOfInvocations
System.out.println(numberOfInvocations)
public static int numberSoFar( )
numberOfInvocations return
numberOfInvocations
18Wrapper Classes
- Sometimes a primitive value needs to be passed in
as an argument, but the method definition
requires an object as the corresponding formal
parameter. - The java.lang package contains wrapper classes
that correspond to each primitive type
19Wrapper Classes
- The following declaration creates an Integer
object which represents the integer 40 as an
object - Integer age new Integer(40)
- An object of a wrapper class can be used in any
situation 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
20Wrapper Classes, cont.
- To retrieve the integer value
- int i n.intValue()
- primitive type wrapper class extraction method
- int Integer intValue
- long Long longValue
- float Float floatValue
- double Double doubleValue
- char Character charValue
21Shorthand in Java 5.0
- Wrapping is done automatically in Java 5.0.
- Integer n 42
- which is equivalent to
- Integer n new Integer(42)
- Similarly
- int i n
- is equivalent to
- int i n.intValue
22Automatic Boxing and Unboxing
- Converting a value of a primitive type to an
object of its corresponding wrapper class is
called boxing. - Integer n new Integer(42)
- Java 5.0 boxes automatically.
- Integer n 42
23Automatic Boxing and Unboxing, cont.
- Converting an object of a wrapper class to a
value of the corresponding primitive type is
called unboxing. - int i n.intValue
- Java 5.0 unboxes automatically.
- int i n
24Automatic Boxing and Unboxing, cont.
- Automatic boxing and unboxing also apply to
parameters. - A primitive argument can be provided for a
corresponding formal parameter of the associated
wrapper class. - A wrapper class argument can be provided for a
corresponding formal parameter of the associated
primitive type.
25Useful Constants
- Wrapper classes contain several useful constants
and static methods such as - Integer.MAX_VALUE
- Integer.MIN_VALUE
- Double.MAX_VALUE
- Double.MIN_VALUE
26Type Conversions
- Static methods in the wrapper classes can be used
to convert a string to the corresponding number
of type int, long, float, or double. - String theString 199.98
- double doubleSample
- Double.parseDouble(theString)
- or
- Double.parseDouble(theString.trim())
- if the string has leading or trailing whitespace.
27Type Conversions, cont.
- Methods for converting strings to the
corresponding numbers - Integer.parseInt(42)
- Long.parseLong(42)
- Float.parseFloat(199.98)
- Double.parseDouble(199.98)
28Type Conversions, cont.
- Methods for converting numbers to the
corresponding strings - Integer.toString(42)
- Long.toString(42)
- Float.toString(199.98)
- Double.toString(199.98)
29Static Constants in Class Boolean
- The constants in wrapper class Boolean include
- Boolean.TRUE
- and
- Boolean.False
- but the keywords true and false are much easier
to use.