Java Reference
In-Depth Information
• Parameters are declared in a comma-separated parameter list (p. 73), which is located inside the
parentheses that follow the method name in the method declaration. Multiple parameters are
separated by commas. Each parameter must specify a type followed by a variable name.
• Variables declared in the body of a particular method are local variables and can be used only in
that method. When a method terminates, the values of its local variables are lost. A method's pa-
rameters are local variables of the method.
• Every method's body is delimited by left and right braces ( { and } ).
• Each method's body contains one or more statements that perform the method's task(s).
• The method's return type specifies the type of data returned to a method's caller. Keyword void
indicates that a method will perform a task but will not return any information.
• Empty parentheses following a method name indicate that the method does not require any pa-
rameters to perform its task.
• When a method that specifies a return type (p. 73) other than void is called and completes its
task, the method must return a result to its calling method.
•The return statement (p. 74) passes a value from a called method back to its caller.
• Classes often provide public methods to allow the class's clients to set or get private instance
variables. The names of these methods need not begin with set or get , but this naming convention
is recommended.
Section 3.2.2 AccountTest Class That Creates and Uses an Object of Class Account
• A class that creates an object of another class, then calls the object's methods, is a driver class.
Scanner method nextLine (p. 75) reads characters until a newline character is encountered, then
returns the characters as a String .
Scanner method next (p. 75) reads characters until any white-space character is encountered,
then returns the characters as a String .
• A class instance creation expression (p. 75) begins with keyword new and creates a new object.
• A constructor is similar to a method but is called implicitly by the new operator to initialize an
object's instance variables at the time the object is created.
• To call a method of an object, follow the object name with a dot separator (p. 76), the method
name and a set of parentheses containing the method's arguments.
• Local variables are not automatically initialized. Every instance variable has a default initial val-
ue—a value provided by Java when you do not specify the instance variable's initial value.
• The default value for an instance variable of type String is null .
• A method call supplies values—known as arguments—for each of the method's parameters. Each
argument's value is assigned to the corresponding parameter in the method header.
• The number of arguments in a method call must match the number of parameters in the method
declaration's parameter list.
• The argument types in the method call must be consistent with the types of the corresponding
parameters in the method's declaration.
Section 3.2.3 Compiling and Executing an App with Multiple Classes
•The javac command can compile multiple classes at once. Simply list the source-code filenames
after the command with each filename separated by a space from the next. If the directory con-
taining the app includes only one app's files, you can compile all of its classes with the command
javac *.java . The asterisk ( * ) in *.java indicates that all files in the current directory ending
with the filename extension “ .java ” should be compiled.
Search WWH ::




Custom Search