Method Calling
–
Instance Methods Vs. Class Methods
For Instance!
In object

oriented programming (Java is an object

oriented programming language), an
instance
is a reference

type
variable. For example, in the line of code below, the variable
name
is an
instance
of a String.
String name = “Blah McBlahington”;
An instance is a particular String object that has specific content (“Blah McBlahington” above) whereas a String, in
general, is just some collection of symbols. The String
class
describes what it m
eans to be a String (some collection of
symbols, enclosed in quotation marks) but an
instance
of String will have some specific arrangement of symbols (like the
name
variable above). We could say that
name
is an instance of class String. When we say “for i
nstance” in everyday
conversation, we mean that we have an example of some generic idea; in programming, an instance is a specific variable
of a generic class
(a class is a generic description of the idea)
.
More Class!
A class describes a type of object. F
or example, we could try to define a table (a real

world table that you might eat
dinner on). Our description might include things like “flat surface” and “four legs”. The description is not actually a table
,
though. We would not want to eat dinner on our
description. An actual table, like the one in my dining room, is an
instance
; the description is a
class
. How about another example? My house has blueprints that described how to build
my house. The blueprints describe, in detail, what materials to use as
well as the dimensions of the walls, floors, ceilings,
etc. From the blueprints, you can build many houses. However, you couldn’t live in the blueprints or seek shelter from a
rainy day under the description of the house’s roof. The blueprints represent a
class
; my house is an
instance
of that
class.
Instance Methods
Instance methods are methods that you call on an instance…you call them on a variable.
You have seen several
examples of this with String methods. Here is an example:
String color = “purplue”;
int count = color.length();
String sub = color.substring( 3 );
In the code above, the length() method is called on the
color
variable…
color
is an
instance
of the String
class
. You call the
method on the
instance
to find out the length of that particular
String. Since all Strings can have different lengths, you
must ask the particular
instance variable
for its length. In the code above, the length of
color
is 7, but if we had assigned
“red” to
color
then the length would be 3.
Instance methods need the da
ta of the particular, specific instance in order to function.
It wouldn’t make sense to ask
for the length of a generic String; the generic notion of a “collection of symbols” can’t have a specific length. However, a
specific String variable will have a sp
ecific collection of symbols; that specific instance of a String will have a length.
Class Methods
A class method does not need to be called on an
instance variable
; it can be called upon the class itself. We have seen
this with the JOptionPane methods
showMessageDialog
and
showInputDialog
.
Here is an example of using JOptionPane
to show a message to a user:
JOptionPane.showMessageDialog( null, “You are totally reading this!” );
Notice that we don’t have an
instance
of a JOptionPane; we do not have a JOp
tionPane variable. Instead, we call the
method on the
class
(or the reference type).
Compare th
is
to our String methods; we don’t call the length method on String itself, but on a variable of
type String.
The Math Class
The Math class has many useful methods in it. These methods mirror a lot of common mathematical functions. Here is
the
method
header
for the
sqrt
method, which is really the
square root
function.
double sqrt( double
x
)
A
method header
fits the followin
g pattern:
returnType methodName( Type1 parameter1, Type2 parameter2, … )
The parts are described here:
returnType
The type of information that is returned. Some methods do not return any information; you will see
void
here if
the method does not re
turn information.
methodName
The name of the method. This typically describes the function of the method, but the name can be arbitrary.
ParameterList
( Type1 parameter1, Type2 parameter2, … )
The parameter list is completely optional. Some methods do no
t require any information, but those that do will
have parameters in their method header. The parameter list tells you 1) how many parameters and 2) what
type(s) of parameters. If a method requires parameters, you must send the correct number and type of
p
arameters and you must list them in the order stated in the method header.
So,
now that you know how to read a method header, let’s look at the
sqrt
method again:
double sqrt( double
x
)
This method is named
sqrt
and requires a single decimal parameter
(you can also send an int). The method returns a
double (the approximate square root of the parameter). This is a
class method
so we do
not
need an
instance
of Math to
call the method; we can call it on the Math class itself. For example:
double rootTwo
= Math.sqrt( 2.0 );
//you can send a literal double to the method
double rootOfRootTwo = Math.sqrt( rootTwo );
//or you can send a double variable to the method
Every method in the Math class is a
class metho
d
so you will always use Math.methodName(
) for these methods. Here is
a list of other methods in the Math class.
int
abs( int x )
returns the absolute value of the int parameter
double
abs( double x )
returns the absolute value of the double parameter
double
pow( double base, double
exp )
returns the power of base raised to exp [Math.pow( 2, 3) is 2 cubed]
double
sqrt( double x )
returns the square root of the double parameter
double
min( double a, double b )
returns the smaller of the two parameters
int
min( int a, int b )
an int version of the method above
double
max( double a, double b )
returns the larger of the two paramters
int
max( int a, int b )
an int version of the method above
double
ceil( double x )
returns the next integer that is greater than or equal t
o the parameter
double
floor( double x )
returns the greatest integer that is less than or equal to the parameter
long
round( double x )
returns the double parameter as a rounded integer…
as a long!
double
random()
takes no parameters, returns a random number
between 0.0 and 1.0 (could equal 0.0, must be less than 1.0)
NOTE: the
round
() method
returns
a long…you will need to cast that to an int if you want to use this method. See below.
Again, these methods are all
class methods
so you call them on the Math class itself
;
you will never declare a Math
variable
. Here are some examples that use the following variables:
int
numb
= 5
,
foo
= 3
;
double
dbl
=

5.7
,
trbl
= 4.2
;
int
rounded
= (in
t) Math.round(
dbl
) ;
//
Note the cast to an int
…required…
rounded
is now

6.0
double
cubeIt
= Math.pow(
numb
,
foo
);
//
cubeIt
is now 5
3
or 125…as a double, so 125.0
int
small
er
= Math.min(
numb
,
foo
);
//
smaller
is now 3, the smaller of the two
double
big
= Math.max(
dbl
,
trbl
);
//
big
is now 4.2, the larger of the two
double
flour
= Math.floor(
dbl
);
//
flour
is now

6.0, the largest integer less than

5.7
int
tower
= (int) Math.floor(
dbl
);
//
tower
is now

6…an int because of the
cast
int
rand
=
(int) ( Math.random() * 6 ) + 1;
//
rand
is now a random number between 1 and 6
int
d20
= (int) ( Math.random() *
20
) + 1;
//
d20
is now a random number between 1 and 20
Note
:
0
<
Math.random()
< 1
so if you multiply
all three parts
by 6, you get
0
<
Math.random() * 6
< 6
casting this (cutting off the decimal) gives from 0 to 5
0
<
(int)( Math.random() * 6 )
<
5
note could = 0, 1, 2, 3, 4, or 5…so now add 1 to get 1
–
6
1
<
(int)(Math.random()*6
) + 1
<
6
multiply by a different number
and you get a diff. range
There are other methods in the Math class; read Lesson 6 of Blue Pelican Java to find out how to use the natural log
function, trig functions, inverse trig functions, etc.
Comments 0
Log in to post a comment