Study guide: Object-oriented language basics, Part 2
Brush up on Java terms, learn tips and cautions, and review homework assignments
By Jeff Friesen, JavaWorld.com, 05/04/01
Glossary of terms
- arguments
- A comma-delimited list of expressions passed to a method.
- call-by-reference
- An approach to passing method arguments that passes the address of an argument's variable to the method. That implies the
method does not receive a copy of the argument value, but receives the address of that value's storage location -- and can
modify the original value.
- call-by-value
- An approach to passing method arguments that makes a copy of each method argument and passes that copy to the method.
- caller
- Code that calls a method.
- class field
- A field declared with the
static keyword modifier. Class fields are associated with classes, not objects.
- class method
- A method that accesses the class fields declared in its class.
- constant
- A read-only variable.
- constructor
- A special method that initializes an object.
- enumerated data type
- A reference data type with a restricted set of values.
- field
- A variable declared in a class body that holds either part of an object's state (an instance field) or part of a class's state
(a class field).
- instance field
- A field declared without the
static keyword modifier.
- instance method
- A method that accesses the instance fields and class fields declared in its class.
- local variable
- A variable local to a method block.
- method
- A named body of code declared within a class. Methods describe the behaviors of either an object or a class.
- overloading
- Declaring multiple methods in the same class and with the same name but with different parameter lists.
- parameter
- A named storage location on the method call stack that receives a copy of an argument value at the beginning of a method call.
Tips and cautions
These tips and cautions will help you write better programs and save you from agonizing over why the compiler produces error
messages.
Tips
- When declaring a class, think carefully about which fields and methods should be exposed, and which fields and methods should
be hidden (by declaring them with the
private keyword). As a rule, field/method members that exist to support exposed methods should be hidden. By making those members
hidden, you can later modify them to improve the class's performance, memory utilization, and so on, without breaking external
code that interacts with that class.
- Use constants instead of hard-coded magic numbers. In source code, changing a constant's initial value is easier and less
error-prone than replacing all occurrences of a hard-coded magic number. Also, constants facilitate reading and understanding
source code. For example, the constant
NUM_ARRAY_ELEMENTS is more meaningful than the number 12.
- Use an enumerated type whenever you need a reference type with a restricted set of values and you need to distinguish (efficiently,
via
== or !=) between objects you create from that enumerated type.
- Whenever you encounter a situation where code should only be able to create a single object from some class, use a singleton.
Cautions
- An attempt to directly access an instance field from a class method results in a compiler error.
- The compiler reports an error if code attempts to modify an initialized constant.
- It is an error for code to return a value from a method with a
void return data type. Similarly, it is an error for code not to return a value from a method with any other return data type.
- Code must explicitly initialize local variables before trying to access their contents. Also, it is an error for code to attempt
access to a local variable prior to that variable's declaration.
- You cannot overload a method by only changing return data types.
Homework
For homework, work on the following exercise and answer the following questions: