Java Data Types: Object

Instructor: Thomas Wall

Thomas is a professional software developer, online instructor, consultant and has a Masters degree.

You'll learn about Java objects and the class descriptions which define them. In object-oriented programming, we design and implement a program using objects and classes, manipulating them to achieve your goal.

Object Orientation

Java objects are analogous to objects in the real world. In the real world, an object is an entity having both state and behavior. It can be physical (tangible) , such as a table, pen, chair, car, etc., or logical (intangible), such as a bank account. For example: The pen on my desk is an object. It's name is Bic; color is white; length is 5 inches, etc. These parameters are its state. One of its behaviors is that it's used for writing. An emotion is NOT an object because it can't be precisely specified by state and behavior.

In Java, all objects are intangible in the sense they only exist as a collection of data and programming in computer memory. A Java object is a representation of an entity and an implementation of its behaviors. A Java object, such as Automobile, is only a representation of a real world automobile and a simulation of its behavior. All Java objects have three characteristics:

  • State is determined by a group of data values associated with the object (such as make, model, color, number of doors, horsepower, passenger capacity, etc.).
  • Behavior is a series of methods that implement the object's functionality (such as start, accelerate, decelerate, turn, etc.).
  • Identity is a unique ID used to identify the specific object from all other objects of the same kind (analogous to a real car's VIN number). This ID is used internally by the Java system to reference and track an object and is not directly changeable by the programmer or user.

Classes in Java

Objects and classes are intertwined. A class is a description of a group of objects that have common properties and behavior. It's a template or blueprint from which specific objects are created. An object is a specific instance of class with its own state. A Java class declaration describes these essential components of any object of the class:

  • Data members: Variables that represent the state of the object.
  • Methods: Programming code that implements the various object behaviors.
  • Constructors: Special methods that initialize an object's data members using values passed to them.

Java is an object-oriented programming language. For a Java program to accomplish some task, the programmer must declare some class descriptions (or utilize one the many built-in classes), construct some specific objects from those descriptions, and then call these object's methods to perform various actions. Here's the declaration of a simple Rectangle class:

// Declare a simple Rectangle class
class Rectangle {
  // A rectangle's state is determined by its height and width
  private double height;
  private double width;
  // Construct from supplied parameters
  public Rectangle (double h, double w) {
   height = h;
   width = w;
  }
  // Computation methods
  public double Perimeter() {
   return 2.0 * (height + width);
  }
  public double Area() {
   return height * width;
  }
}

As the Java comments indicate, a Rectangle's state is determined by its height and width. Methods exist to compute (and return) the perimeter or area of a Rectangle. It's mandatory there be a special method (called a constructor) whose name is the same as the class name. This special method's job is to initialize the state of a newly created Rectangle object using information passed into the method. In our example, the Rectangle constructor uses two values passed into it (h and w) and uses them to set the initial values of the Rectangle object's height and width.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support