CHAPTER 2

Additional Features of Programming and Java

In Chapter 1, the primary goal was to introduce object-oriented concepts, such as interfaces, inheritance and polymorphism, in the context of the Java programming language. This chapter introduces more topics on programming in general and Java in particular, and illustrates how they can aid your programming efforts. For example, Java's exception-handling mechanism provides programmers with significant control over what happens when errors occur.

CHAPTER OBJECTIVES

  1. Distinguish between static members and instance members.
  2. Be able to develop JUnit tests for a class's methods.
  3. Be able to create try blocks and catch blocks to handle exceptions.
  4. Compare file input/output with console input/output.
  5. Understand the fundamentals of the Java Virtual Machine.
  6. Be able to override the Object class's equals method.
  7. Understand the interplay between packages and visibility modifiers.

2.1 Static Variables, Constants and Methods

Recall, from Section 1.2.2, that a class member is either a field or method in the class1. Let's look at some of the different kinds of members in Java. There are two kinds of fields. An instance variable is a field associated with an object—that is, with an instance of a class. For example, in the FullTimeEmployee class from Chapter 1, name and grossPay are instance variables. Each FullTimeEmployee object will have its own pair of instance variables. Suppose we declare

FullTimeEmployee oldEmployee, currentEmployee, ...

Get Data Structures and the Java Collections Framework, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.