Sie sind auf Seite 1von 11

Ch.

3: Methods - Terminology
A method which calls another method is called a calling method or invoking method.

public void someMethod() // calling method
{
int argument1 = 1, argument2 = 2;
someOtherMethod(argument1, argument2);

}

public void someOtherMethod(int paramter1, int paramter2) //called method
{
}
A method which is being called is a called method or invoked method.
The information passed by the calling method to the called method is passed through arguments.
The information received by the called method from the calling method is received by parameters.
The arguments and parameters must match in their data types, order, and number. So you cant send in two
integers if you are supposed to only send in one float.

General Syntax for a method:
public returnType methodName (paramter list)//this is the header
{} //this is the body
returnType is: int, double, float, char. (see mutator and mutator call in RoomType.java)
Ch 3.1 - Reusing Method Names
Java provides the capability of using the same method name for
more than one method, which is referred to as method
overloading(see RoomType.java for constructor overload & use)
3.1 - Passing a reference Value
When passing values to a method, the argument values are copied to
temporary storage locations, and the method parameters have access to
the copies. If the parameters are changed in the called method, the
arguments passed will not change; only the temporary copies are
changed.

public void someMethod() // calling method
{
float argument1 = 1;
float argument2 = 2;
someOtherMethod(argument1, argument2);
}

public someOtherMethod(float paramter1, float paramter2) //called method
{
paramter1=2;//doesnt change argument1
paramter2=4;//doesnt change argument2
}



3.2 - Returning a Single Value
Methods can return int, char, double, float, or void (if not returning
anything)
public void methodName(int var1, int var4)
{
int someNum;
//someNum = var1*var4;
someNum = methodName2(var1, var4);
}

public int methodName2(int x, int y)
{
return (x*y);
}
Java does not want you to return more than one value from a method.

3.3 Method Development: Algorithms
An algorithm is a step-by-step set of instructions that
describes how data are to be processed to produce the
desired result. Its your own human recipe that you
need to follow in order to write a computer recipe
(program).
Most people dont think algorithmically, they tend to
think intuitively. If you had to change a flat tire, you
would not think of all the individual steps, you would
just change the tire, or call someone to do it for you.
Flowcharts can help visualize big complex problems
and their algorithms.
3.4 Swapping Values
Swapping values between two variables is
easy. All you have to do is declare a third
variable to temporarily hold a value:
public void swap(int num1, int num2)
{
int temp;
temp = num1;
num1=num2;
num2=temp;
}


Ch3.5Remember Ch2.2
Classifications of Variables
Instance Variable:
is placed within a classs body, and outside any method
does not contain the static reserved word
Class Variable:
is placed within a classs body, and outside any method
does contain the static reserved word
Local Variable:
is placed within a methods body
does contain the static reserved word
Parameter Variable:
within the parenthesis of the methods header line
(Note: static variable is created only once for each class more in
Chapter 3 p.160)

3.5 static Variables
It is convenient in some cases for all objects to share the same memory location
for a specific variable. For example an employee record system where all
employees are subject to the same single variable for tax rateor another same
single variable to count the number of employees.
This is why java has static variables. A static variable is created only once for each
class. It is shared by all objects created from the class and exist even if no object is
instantiated (see the last page of this slide to review creating objects)
A variable declared as static belongs to a class as a whole. Thus, a static variable is
created only once, no matter how many objects of the class are created, and it
exists even if no objects are created. Each object shares the same static variable
and a change to the static variables value by one object effectively changes the
value of the static variable for all objects. (See Program3.10 on page 161, it creates
two objects which share same static variables for tax rate & employee count)
Because static variables exist outside any specific object, Java provides a means of
accessing such variables independently of any objectthis is the purpose of a
static method. A static method can be invoked independently of any object from
inside the class itself (See program 3.13 on page 165notice that the call to
average() is not preceded by an object name such as objectName.average() .
None is required because average() is a static method. It does not require a class
name, because it is called from within the class that defines it.
A static method can be called from any outside class by providing an object or a
class name. (See programs 3.11 and 3.12 on page 163-164 to see how you code
static methods, 3.12 shows how static methods are called using a class name, not
the object name, unlike regular methods)
Ch3.5 - final variables
Example:
public final static double PI =3.1416;

final variables are used for constants such as PI,
gravity and other things you dont want to
change.
3.5 Scope and Visibility
The section of a code within which an identifier is valid is
referred to as the scope.
Variable declared within a method have a local scope
Class scope all instance and static variables declared in
the class variable declaration section and all methods
contained within the classs method definition section
In addition to scope, classes and their members have
visibility, which determines whether the member can be
accessed from outside the class in which it is declared
(public, private, protected)
Remember Ch2.2 - Creating Objects
Declaring an object to be of a class type is similar to declaring a variable of a
primitive data type such as int or double. With one extra step

RoomType roomOne;//declare a variable roomOne to be a RoomType data type
roomOne = new RoomType();//create an actual object that can be referenced by the
roomOne variable.so roomOne is known as a reference variable

//this can be written on one line:
RoomType roomOne =new RoomType();

new is referred to as the dynamic memory allocation operator
Formally the process of creating a new object using new is referred to as creating
an instance or instantiating an object

Das könnte Ihnen auch gefallen