Java Reference
In-Depth Information
else {
/* print error here */
return -1;
{
You could use the simpler logic to implement the credit() method, which checks if the amount is valid, instead
of checking if the amount is invalid. I did not use this logic because I wanted to demonstrate in the same example how
to use a private method. Sometimes one writes more code to drive home a point in a discussion.
Now you are left with the protected access level modifier. When do you declare a class member protected ? A
class member with the protected access level can be accessed in the same package and in the descendant class, even
if the descendant class is not in the same package. I will discuss how to create a descendant class and the use of the
protected access level in the chapter on inheritance.
Parameter Passing Mechanisms
This section discusses different ways of passing parameters to a method that are used in different programming
languages. I will not discuss anything that is specific to Java in this section. The syntax or symbols used in this section
may not be supported by Java. This section is important to programmers in understanding the memory states in
the process of a method call. If you are an experienced programmer, you may skip this section. The next section will
discuss the parameter passing mechanisms in Java.
The following are some of the mechanisms to pass parameters to a method:
Pass by value
Pass by constant value
Pass by reference
Pass by reference value
Pass by result
Pass by result value
Pass by name
Pass by need
A variable has three components: a name, a memory address (or a location), and data. The name of a variable is
a logical name that is used in a program to refer to its memory address. Data is stored at the memory address that is
associated with the variable name. The data stored at the memory address is also known as the value of the variable.
Suppose you have an int variable id whose value is 785 , which is stored at the memory address 131072 . You may
declare and initialize the id variable as follows:
int id = 785;
You can visualize the relationship between the variable name, its memory address, and the data stored at the
memory address as depicted in Figure 6-4 .
 
Search WWH ::




Custom Search