Java Methods

advertisement
Java Methods
Q: What is a method?
A: It is a named piece of code to which we can pass parameters and it may return a value to us (1
of the 8 primitive data types or an object reference) or it may return nothing.
Q: Why do we want to use methods?
A: Several reasons actually…
1. It allows us to use the same piece of code in several places without using block copy
(which would lead to us having to maintain several copies of it). Thus it reduces code
size by supporting code-reuse.
2. It nicely supports abstraction, meaning that we can call up the code and use it
without having to know all of the inner details of how it actually works. For example,
we could ask it to sort a collection of values and know that when execution returned
to us that the job would be done without having to know exactly how the sorting was
accomplished.
3. The writing of methods nicely supports modularity, meaning that large projects can
be broken down into more manageable pieces that can potentially be developed in
parallel by a team of programmers.
Q: Are there more than one flavor of methods?
A: Yes, 3 kinds of methods actually.
1. A class method (we always see the keyword static used in the method header of such
methods). These methods are viewed as being supplied by the class to any clients of
the class. These methods in essence do not have any actor object to perform them,
thus they may not (a) use any of the instance variables and may not use the keyword
this (to be discussed much later). Examples of such a method that you have already
written is the method called main that has appeared in all of your classes. Clients of
some class may invoke the class method by naming both the class providing the
method as well as the name of the class method and any arguments (actual
parameters) that may need to be passed to the method.
For example: consider the line:
userInput = JOptionPane.showInputDialog(“Enter a number”);
In the above line the programmer who wrote this line is a “client” of the JOptionPane
class. In particular, the programmer is calling upon (“invoking”) the class method
called: showInputDialog. The programmer is passing a single argument (actual
parameter) to that method, namely the String “Enter a number”. The
showInputDialog class method is a String-returning method (technically it returns an
object reference to a String), which is then stored into the variable userInput.
As a second example of a call to a class method consider the line:
userInt = Integer.parseInt(userInput);
In the above line the programmer who wrote this line is acting as a client of the
Integer class and is calling upon the class method whose name is parseInt. One
argument (actual parameter) is being passed to that method, namely the variable
caller userInput. The parseInt method returns an int to the calling code.
2. An instance method (this will be a method without static in its method header). We
view an instance method as being executed by an object of the given type. We can
think of this object as the “actor” being asked to do the given method. In instance
methods we may use instance variables (namely those that pertain to the actor object).
We may also use the keyword this in such a method (more on that later!)
In the case of an instance method call, we name the actor-object and then we
name the method we want invoked and then if any arguments are called for we supply
those too. As was the case with class methods, instance methods may be void, may
return one of the eight primitive data types, or may return an object reference.
For example, consider the lines:
String myString = “Help”; // myString is an object reference.
int howLong = myString.length();
char thirdChar = myString.charAt(2); // first char uses 0
In the above code we see two calls to instance methods. In both cases the “actor”
object is the object reference that is before the dot (namely myString). We are invoking
the method called length which has no arguments and returns an int. In essence it is like
we are saying “Excuse me Mr. myString, would you please tell me your length?” In the
last line above we are calling upon yet another String instance method called charAt
which takes a single argument of type int (and that int must be between 0 and the length
of the given string – 1). In this case we are passing in the argument of 2. It is like we are
saying “Ms. myString would you please tell me what your third character is?” This
method call returns a char to the caller (in the case above it would return an “l”). If you
tried to ask the String class what is your length or what is your third character the
question would not make sense as individual Strings can answer that about themselves.
In the two lines above it is like myString is the actor object.
3. A constructor which is a method that is used whenever we call upon a class to create
a new object of the given type. For example, in the line:
Human me = new Human(“David”, 21);
The programmer who writes this line is a client of the class called Human and is in
fact asking that a new Human be created (i.e., that an object of type Human be created out
on heap space and that an object reference to that new Human be returned so that it can be
stored into the object reference called “me”. Thus, what follows new is a call to a
constructor method written by the programmer who created that class. In the case above,
the client code is supplying two arguments to constructor call, namely the String David
and the int 21. The values supplied to a constructor call are almost always used to
initialize the instance variables of the object now being created. In the above case we can
imagine that “David” is being used to set an instance variable of type String (maybe
“name”) and that the 21 is being used to set an int instance-variable (maybe one called
“age”). The line: String words = “Hello Mom”; is just a shortcut for the line:
String words = new String (“Hello Mom”); // A call to a constructor is used here.
NOTE: so far we have only been the client of classes and have not created any classes
from which we would create objects (you will see that later in CIS 121).
Inside of a class definition one can spot a constructor (there can be more than one of
them) by the fact that the method has the same name as the class and by the fact that there
is no “return type” because constructors always return an object reference (namely to
the newly created object out on heap).
Q: What is a method (definition) made up of?
A: Two parts:
1. A method header and
2. A method body.
Q: What are the parts that make up a method header?
A: Several things may be there, some of which are optional.
1. An access control keyword like public (this lets everybody use the method) and
private (that severely limits access to the method).
2. The keyword static may or may not be present. If it is there, the method is a class
method. If it is absent you either have an instance method or a constructor.
3. A return type which can be any one of the 8 PDT or can be the name of a class
meaning that an object reference to an object of that type is being returned. If nothing
is being returned the keyword void is used to express that.
4. The name of the method comes next. In the case of a constructor this must be exactly
the same as the class name. For class and instance methods the method names should
be spelled with a lower case letter in the first word and subsequent words should start
with a t capital letter.
5. A formal parameter list. In the call statement we call them arguments or actual
parameters. We MUST have the “( )” even if there are no formal parameters to the
method. Inside of the ( ) we have a list of parameter-type and parameter-name
separated by commas. For example (String name, int age).
Q: What does a method body look like?
A: It starts with a “{“ and ends with a “}”. In between those we can use a sequence of any Java
statements. If the method is supposed to return a value of some kind then the method body needs
to have one or more return statements to specify which of the values is to go back as the value of
the method to the method caller. For example: return answer; or return (10); When one hits
the return statement execution transfers directly back to the caller code. If the method is void,
then the method body need not have a return statement, but when you reach the last line of the
method body it is as if there were a return; there.
Download