Richard G Baldwin (512) 223-4758, baldwin@austin.cc.tx.us, http://www2.austin.cc.tx.us/baldwin/

Defining a Class

Java Programming, Lecture Notes # 12, Revised 10/03/99.

Preface
Introduction
Defining a Class in Java
Defining a Class in C++
Review Questions for Lesson 12

Preface

Students in Prof. Baldwin's Introductory Java Programming classes at ACC are responsible for knowing and understanding all of the material in this lesson (except that they are not responsible for detailed information that is specific to C++).

The detailed material on C++ is provided as supplementary material for the benefit of those persons who are already familiar with C++ and who are making the transition into Java.

Introduction

New Types

Both Java and C++ make extensive use of classes. When a class is defined in either language, a new type comes into being. The new type definition can then be used to instantiate one or more objects of that new type.

A Blueprint

In both languages, the class definition provides a template or blueprint, which describes the data contained within, and the behavior of objects instantiated according to the new type.

The Data

The data is contained in variables defined within the class (often called variables, data members, or attributes).

The Behavior

The behavior is controlled by methods defined within the class (often called member functions or functions in C++).

State and Behavior

An object is said to have state and behavior. At any instant in time, the state of an object is determined by the values stored in its variables and its behavior is determined by its methods.

Class vs. Instance

In both languages, it is possible to define:

Instance variables and instance methods can only be accessed through an object of the class.

Class variables and class methods can be accessed without first instantiating an object.

The class name alone is sufficient for accessing class variables and class methods by joining the name of the class with the name of the variable or method using a period.

(C++ uses the scope resolution operator as the joining operator for accessing class variables and methods).

Defining a Class in Java

The general syntax for defining a class in Java is shown below.
 

class MyClassName{
  . . .
} //End of class definition.

This syntax defines a class and creates a new type named MyClassName.

The definitions of variables and methods are inserted between the opening and closing braces.
 

C++ programmers should note that there is no semicolon following the closing brace in a Java class definition (although placing a semicolon there doesn't seem to cause a problem with the JDK 1.1.1 compiler).

.
 

Defining a Class in C++ 

The general syntax for defining a class in C++ is shown below. 
 

class MyClassName{
  . . .
}; //End of class definition.  Note the required semicolon.

At this level, the syntax for the class definition for both languages appears to be the same except for the requirement for a terminating semicolon in C++. However, there are numerous syntax differences interior to the class definition, which will be discussed, in subsequent lessons. 

.

Review Questions for Lesson 12

Q - List two of the many names commonly used for variables defined within a class in Java.

A - Instance variables and attributes.

Q - List two of the many names commonly used for the functions defined within a class in Java.

A - Member functions and instance methods.

Q - An object is said to have state and behavior. At any instant in time, the state of an object is determined by the values stored in its ___________ and its behavior is determined by its __________.

A - instance variables, methods

Q - What keyword is used to cause a variable or method to become a class variable or method in Java?

A - static

Q - Instance variables and instance methods can only be accessed through an object of the class in Java: True or False?

A - True

Q - In Java, the class name alone is sufficient for accessing class variables and class methods by joining the name of the class with the name of the variable or method using a colon: True or False?

A - False. A colon is used in C++ while a period is used in Java.

Q - Show the general syntax of an empty class definition in Java.

A - class NameOfClass{}

Q - As with C++, the syntax for a class definition in Java requires a semicolon following the closing brace: True or False?

A - Java does not require the use of a semicolon following the closing brace in a class definition.

-end-