Object Programming  «Prev  Next»
Lesson 5Defining objects with classes
ObjectiveClasses and their relationship to objects.

Defining Objects with Classes

You learned previously that a class is a template, or specification, that defines a type of object. It is important to understand the theoretical relevance of classes, but how does that translate into code?
Here is a simple class definition:

class Lion {
  int energy;
  int aggression;
  boolean hungry;
  Color color;
}

This class is named Lion and might be suited for a zoo simulation applet. The keyword class precedes the class name in the class definition; this is how all Java classes are defined.
The state of the Lion class is defined by four data members, which represent the energy, aggression, hunger, and color of the lion. The fact that these variables appear within the curly braces ({}) indicates that they are member variables of the Lion class. Incidentally, you will learn more about data types a little later in the course.Now that you have seen a specific class definition.
The following class diagram contains the general syntax used to define all classes:

class ClassName{
  ClassBody
}
  1. An identifier that specifies the name of the class
  2. The code that defines the functionality of the class, and includes member variables and methods for the class
An identifier that specifies the name of the class

Purpose of Class in Java

A class is the fundamental building block of object-oriented programs. It generally represents a real-world object. A class definition in Java consists of member variable declarations and method declarations and begins with the class keyword. The body of the class is enclosed with brackets and contains all instance variables and methods:

class classname {
// define class level variables
// define methods
}

A pair of open and close curly braces constitutes a block statement. This is used in many other parts of Java.

Classes and objects

A class is a pattern or template for creating multiple objects with similar features. It defines the variables and methods of the class. It declares the capabilities of the class. However, before these capabilities can be used, an object must be created. An object is an instantiation of a class. That is, an object is composed of the memory allocated for the member variables of the class. Each object has its own set of member variables.

What occurs when a new object is created?
  1. The new keyword is used to create an instance of a class
  2. Memory is physically allocated for the new instance of the class
  3. Any static initializers are executed (as detailed in the Java initialization sequence)
  4. A constructor is called to do initialization
  5. A reference to the object is returned

The state of an object is typically hidden from the users of the object and is reflected in the value of its instance variables. The behavior of an object is determined by the methods it possesses. This is an example of data encapsulation.
An object is the instantiation of a class and ach instance of a class has its own unique set of instance variables.