Java Object

Reviewed by: Juliano Rabelo
Last Updated: August 14, 2020

Definition - What does Java Object mean?

A Java object is a combination of data and procedures working on the available data. An object has a state and behavior.

The state of an object is stored in fields (variables), while methods (functions) display the object's behavior. Objects are created from templates known as classes.

In Java, an object is created using the keyword "new".

Techopedia explains Java Object

Java objects are very similar to the objects we can observe in the real world. A cat, a lighter, a pen, or a car are all objects.

They are characterized by three features:

  • Identity
  • State
  • Behavior

For example, a cat’s state includes its color, size, gender, and age, while its behavior is sleeping, purring, meowing for food, or running around like crazy at 4 AM.

Identity

The identity is a characteristic used to uniquely identify that object – for example, the cat’s name. Other simpler objects like a lighter may have only two states (on and off) and behaviors (turn on, turn off), but they still have an identity (that item’s manufacturing ID).

If you look around, you’ll find a lot of examples of objects in the real world that could help you understand what a software object is.

State

A Java object’s states are stored in fields that represent the individual characteristics of that object. For example, in a first-person shooter video game, a pistol with an eight-bullets clip has nine states in total: one for each bullet (e.g. 8 bullets, 7 bullets, 5 bullets, etc.), plus another one when it’s empty (0 bullets).

Behavior

The object’s behavior is exposed through methods that operate its internal state. For example, the “shooting” behavior will change the state of the pistol from “8 bullets'' to “7 bullets” and so forth every time the player shoots with the gun.

The “reloading” behavior will bring back the pistol into the original “8 bullets'' state.

There are three steps to creating a Java object:

  • Declaration of the object.
  • Instantiation of the object.
  • Initialization of the object.

When a Java object is declared, a name is associated with that object. The object is instantiated so that memory space can be allocated. Initialization is the process of assigning a proper initial value to this allocated space. The properties of Java objects include:

One can only interact with the object through its methods. Hence, internal details are hidden. Through states and methods, the objects stay in control of how the world can use it.

For example, any value that is greater than “8 bullets” will be rejected by the method since there’s no state for it. This concept is defined as “data encapsulation.”

When coding, an existing object may be reused. Modularity allows an object to be plugged in in other software and its code to be maintained independently of other objects. At the same time, an object can be plugged off the system whenever it’s necessary.

When a program's operation is hindered by a particular object, that object can be easily removed and replaced. Just like in the real world, you can simply substitute a part of a machine that doesn’t work like a piston or a gear.

A new object t from the class "tree" is created using the following syntax: Tree t = new Tree().

Share this: