Java Reference
In-Depth Information
Methods are subsections of a class that contain executable statements. An executable statement performs an
action (e.g., adding two variables, printing, etc.). Methods can receive and return variables. Methods, like classes,
require a header. A method header has an access modifier and the method name. In addition, the header defines
what data the method expects (is receiving) and what data the method will return. For instance, the following
header defines a method called salaryCalc:
public double salaryCalc( int hoursWorked, double payRate)
The access modifier ( public ) comes first, is followed by a space, and then the type of variable ( double ) that
the method will return. In this case, salaryCalc will return a variable of type double (more on this type of variable
later). There is another space and then the name of the method is next. It is customary to begin method names with
a lowercase letter. The method name is followed by parameters for the values that the method is expecting. The
parameters must be enclosed in parentheses and separated by a comma. Parameters define method variables that
will hold the passed values. Method variables are different from class variables because they have a more limited
scope . In other words, class variables are considered global in scope because any method (within the class) can use
a class variable. Method variables are local in scope because they can only be used in statements within the method
that defines them.
Classes can contain specialized methods. For now, the two specialized methods we will discuss are the main
method and a type of method called a constructor.
The main Method
When you run a class as an application, Java looks for and runs the class's main method. The following is a valid main
method:
public static void main(String[] args) { }
There must be at least one space between each keyword ( public , static , void ) and the method name (main).
Also, the tokens must be in the order shown. You are already familiar with the purpose of the access modifier public .
The keyword void means that the main method does not return anything. String[] args defines the expected data
as an array of String variables called args. One nice feature of Java is that if the person running the class does not
specify an array of strings, Java automatically supplies one at runtime. Finally, the method is defined as static .
A static method does not need any class variables or other methods to work properly. In other words, a static
method can be executed as a “stand alone method.” Usually only one class within an application is coded with
a main method. This single main method creates the objects that comprise the application. In other words,
the main method “kicks off ” the application.
Objects and Instantiation
Most classes are not run as applications; rather, most classes are instantiated . Instantiated is a very intimidating
word. Essentially, when a class is instantiated, a copy (also called an instance ) of the class is placed in the computer's
main memory. A class instance is also called an object . You can actually have many instances of a class or, to say it
another way, many objects of the class type.
For instance, we could instantiate two Employee objects. One object could have a pay rate of ten dollars per hour
and an employee name of “Joe Smith.” The other Employee object's pay rate could be twelve dollars an hour and have
an employee name of “Mary Jones.”
You actually have already instantiated a class (i.e., you have already created an object). A String object was
created when new String(""Joe Employee"") within the following statement was executed:
String empName = new String("Joe Employee");
 
Search WWH ::




Custom Search