Java Objects Form the Basis of all Java Applications

Objects Have State and Behavior

Woman typing
Johner Images/Getty Images

An object in Java — and any other "object oriented" language — is the basic building block of all Java applications and represents any real-world object you might find around you: an apple, a cat, a car or a human.

The two characteristics that an object always has are state and behavior. Consider a person object. Its state might include hair color, sex, height and weight, but also feelings like anger, frustration or love.

Its behavior could include walking, sleeping, cooking, working, or anything else that a person might do.

Objects form the very core of any object-oriented programming language.

What is Object Oriented Programming?

Hundreds of books have been written to describe the intricacies of object-oriented programming, but basically, OOP is based on a holistic approach emphasizing re-use and inheritance, which streamlines development time. More traditional procedural languages, such as Fortran, COBOL and C, take a top-down approach, breaking down the task or problem into a logical, orderly series of functions.

For example, consider a simple ATM application used by a bank. Before writing any code, a Java developer first will create a roadmap or plan on how to proceed, usually beginning with a list of all the objects that need to be created and how they will interact. Developers may use a class diagram to clarify relationships between objects.

Objects required for use in an ATM transaction might be Money, Card, Balance, Receipt, Withdrawal, Deposit and so on.  These objects need to work together to complete the transaction: making a deposit should result in a balance report and perhaps a receipt, for instance. Objects will pass messages between them in order to get things done.

Objects and Classes

An object is an instance of a class: here is the krux of object oriented programming and the idea of re-use. Before an object can exist, a class on which it can be based must exist. 

Perhaps we want a book object: to be precise, we want the book The Hitchhiker's Guide to the Galaxy. We first need to create a class Book. This class could be the basis for any book in the world.

It might look something like this:

public class Book {
   String title;
   String author;

 //methods
   public String getTitle(
    {
      return title;
    }
    public void setTitle()
    {
        return title;
    }
    public int getAuthor()
    {
        return author;
    }

    public int setAuthor()
    {
        return author;
    }   
    // etc.
}

The class Book has a title and an author with methods that allow you to set or get either of these items (it would have more elements as well, but this example is just an excerpt). But this is not yet an object — a Java application can't yet do anything with it.  It needs to be instantiated to become an object that can be used. 

Creating an Object

The relationship between an object and a class is such that many objects can be created using one class. Each object has its own data but its underlying structure (i.e., the type of data it stores and its behaviors) are defined by the class.

We can create several objects from a book class. Each object is called an instance of the class.

Book HitchHiker = new Book("The HitchHiker's Guide to the Galaxy", "Douglas Adams");
Book ShortHistory = new Book("A Short History of Nearly Everything", "Bill Bryson");
Book IceStation = new Book("Ice Station Zebra", "Alistair MacLean");

These three objects can now be used: they can be read, purchased, borrowed or shared.  

Format
mla apa chicago
Your Citation
Leahy, Paul. "Java Objects Form the Basis of all Java Applications." ThoughtCo, Mar. 20, 2017, thoughtco.com/object-2034254. Leahy, Paul. (2017, March 20). Java Objects Form the Basis of all Java Applications. Retrieved from https://www.thoughtco.com/object-2034254 Leahy, Paul. "Java Objects Form the Basis of all Java Applications." ThoughtCo. https://www.thoughtco.com/object-2034254 (accessed September 23, 2017).