Database Reference
In-Depth Information
Methods
In the code for MyApp2 ( MyApp2.java ) you will see three blocks of code named main() , getArray() , and
setRef() . Find them by looking for open and close curly brackets within the curly brackets that define
the body of the class MyApp2 . These three blocks of code are called methods (programmers may think of
them like subroutines or functions).
Each standard method requires two things: a set of parentheses where parameters (input values or
objects) may be passed to the method, and a declaration of the return type (being handed back to
whatever code called the method). When I mention methods in this text, I will append the open and
close parentheses. Later, I will discuss in more detail the public and private modifiers. For now, know
that they are not return types.
Look at the middle method first. getArray() has an empty set of parentheses that indicates it
doesn't take any input parameters. Before the name of the method, it has the word ARRAY , which shows
that it returns an object of type ARRAY .
Notice that both the main() and setRef() methods have a return type of void, which is a way to say
that those methods do not return anything.
The main() method takes a single parameter named args , and the type of that parameter is an array
of Strings . String is the term for a series of characters, like the word “tremendous,” or the sentence,
“This is good!” An array of any type is indicated by open and close square brackets ([ ]). Any single
element in the array can be referenced by placing an integer in the square brackets to indicate its place,
its index. For example, args[0] is the first element of the array of Strings passed to the main() method.
When defining an array, you can put the square brackets on either the type or the name; the following
are identical:
String[] sAr;
String sAr[];
There is another kind of method that is different from the standard methods—it is called a
constructor. Constructors are called when an instance of a class is created in computer memory.
Constructors do not have a return type, and the name of the constructor method is the same as the
name of the class. For example, a constructor for MyApp2 might look like this:
public MyApp2() {
}
Notice that there is no return type indicated. If you do not define a constructor in your code, your
class will use the default constructor, which would look very much like this example constructor. Also
compare this default constructor with the MyApp2 class definition; they are very different things, but bear
some resemblance: public MyApp2() { compared with public class MyApp2 { .
Values
In our example code for MyApp2 , shown in Listing 3-3, we have statements where we set a member
variable equal to a value. For example, we said the following:
ARRAY mA = m.getArray();
In describing this statement, I am as likely to say, “set mA equal to m.getArray() ” as I am to say, “get
m.getArray() into mA. ” But what I should be saying, and what you need to understand, is that the
member variable mA is a pointer to an object of type ARRAY . What I am doing in this statement is setting
the value of the pointer to the address in memory of the object that is returned by m.getArray() . Even
this is not precise. It is more precise to say that m.getArray() is returning a pointer to the location of an
ARRAY, and I am setting the pointer mA to that same location.
 
Search WWH ::




Custom Search