COP2800 : Java Programming Chapter 2

Using Methods, Classes, and Objects

Section A Programming Using Methods

1) Method: A series of statements that carry out some task.
Any class can contain an unlimited number of methods.
Within a class, the simplest methods you can invoke don't require any arguments or return any values.

2) A method must contain these:

3) The Method declaration contains:

4) Most Methods are public.

5) Arguments: They are communication from you.
You need the type and the local name for the argument.
The arguments in a method call often are referred to as the actual parameters.
The variable in the method declaration that accept the values from the actual parameters are the formal parameters.
There may be a single or several arguments in a method but if there are several, they are separated by a comma.
If there are 2 method arguments of the same type: passing them to a method in the wrong order results in a logical error.
If they are of different types, passing them in reverse order constitutes a syntax error.

6) Actual Parameters: The arguments in a method call.
Example: predictRaise (400.00);
the 400.00 is the actual parameter.

7) Formal Parameters: The variables in the method declaration that accept the values from the actual parameters.
Example: public static void predictRaise (double moneyAmount)
moneyAmount is the formal parameter.

8) Return statement: It causes the values stored to be sent back to any method that calls that method. Anything from a simple int to an object of arrays can be returned. A method's return type is known more succinctly as a method's type.
Example: static public void nameAndAddress();
The method is public and it returns no value, so it is type void.

Back to the Top


Section B Using Classes

1) Instantiation: An object is an Instantiation of a class; an object is 1 tangible example of a class. The data components of a class often are referred to as the instance variable of that class. Also, class object attributes often are called fields to help distinguish them from other variables you might use. You can call a program or class that instantiates objects within their own programs. You can call a program or class that instantiates objects of another prewritten class as a class client or class user.

2) Inherit: Objects inherit attributes from classes.

3) Creating a Class - to begin, you create a class header with 3 parts:

Example: public class Employee
pubic is the class access modifier, keyword class, Name.
You place the instance variables, or fields, and methods within curly brackets.

4) Access specifier: if you do not provide an access specifier for a class field, its access is default, which is more liberal access than public.

5) Field modifiers: public, private, protected, static, and final.
They are the same as the method access modifiers with the addition of final.
Most class fields are private, which provides the highest level of security.

6) Private access : is sometimes called information hiding, and is an important component of OOP.

7) static: the key word static is used for classwide methods, but not for methods that "belong" to objects. If you are creating a class from which objects will be instantiated, most methods will probably be NON-static as you will be associating the methods with individual objects.

8) Instance methods: Methods used with object instantiations.

9) new operator: used to reserve memory for an object.
First you declare identifier like: Employee someEmployee;
then you need the new operator to reserve memory...
therefore it becomes: Employee someEmployee= new Employee();
In this statement, Employee is the object's type (as well as its class), and someEmployee is the name of the object.
The new operator is allocating a new, unused portion of computer memory.
The portion after new -> Employee();
is the name of a method that constructs and Employee object.
Employee(); is a constructor method.

10) Calling a method of an object. After an object has been instantiated, its methods can be accessed using the object's identifier, a dot, and a method call.
I.e.: someEmployee.changeSalary(350.00); -> object dot method (actual parameter).

11) Constructor method: is a method that establishes an object.
Any constructor method must have the same name as the class it constructs, and constructor methods cannot have a return type.

12) Default Constructor: i.e. Employee()

You can write any statement or print a message within a constructor but not advised.

Back to the Top


Links to COP2800 Java Programming Chapters:
Links to the java.sun.com to help with Java Programming

Back to the Top