Discovering Java Objects and How They Work

Athena Ozanich
Athena Ozanich


Many programming languages have the concept of objects, but it differs from language to language. Each programming language has its definition of objects; in JavaScript, objects store complex data. Objects in the Java programming language are very different, as they serve many functions; let's discuss why.

Woman taking notes on what Java objects are and how they can benefit her programming.

In this post, you will learn about the Java object, what it is and how to use it. You will also discover how to identify the different parts of a Java object and how to use them. Furthermore, you will see some example code of what a Java object looks like in practice. The post will wrap up with some tips on how to move forward with incorporating and building your understanding of Java Objects.

Without any further delay, let's get this party started.

Download Now: An Introduction to Java & JavaScript

What is an Object in Java

In the Java programming language, an object is an instance of a Java class, meaning it is a copy of a specific class. Java objects have three primary characteristics: identity, state, and behavior. These characteristics are the building blocks of any class object and set the scene for how they are used.

Let’s look at what these three characteristics are and how they work.

  • Identity: The identity of an object is a unique identifier, such as a memory address, ID, or even a name.
  • State: The state controls aspects of an object; in the case of describing a fan, you could have an on, off, low, medium, or high state. State monitors behavior, such as turning a fan on or off, the state will change when the behavior happens.
  • Behavior: Object behavior is used to describe what an object can do, such as a fan turning on or off or changing speeds

You probably notice these three characteristics work together intimately at this point. Without an identity, an object cannot have a state or behavior. Furthermore, an object’s behaviors would be near impossible to maintain without a state.

Let’s discuss how these characteristics of an object work in comparison to real-life objects.

In life, we encounter objects all the time, and Java objects mimic these objects we encounter. Many developers liken the Java object to cars with identities, states, and behaviors. 

A car has a make, model, and VIN; these are all used to identify a vehicle uniquely. A car also has multiple behaviors, off, on, and an automatic has park, drive, neutral, reverse, etc. The question of the state of a vehicle is a little more complicated. As I mentioned, the state tracks current behaviors, so when the car is put into gear, it uses mechanical devices to keep the state of the current gear. This behavior allows the vehicle to perform its behavior consistently.

Java offers these same abilities by creating objects from declared classes, known as members or instances. Let’s discuss this further by looking at the Java class of which they are copies.

What is Class in Java

Java is an object-oriented programming language, meaning everything in Java is an object. Each object has a different name, and a class is unique in that they are used to create blueprints for objects. A class must have a unique name used to create individual class instances. The class informs what states and behaviors its instances can have.

A simplified car class could have the following information, allowing you to modify its state based on behavior and unique identifier.

Car state:

  • On/Off
  • Speed


  • Start/Shutdown
  • Accelerate/Decelerate


  • VIN

Check out this next video on classes from our Java Class blog post.

Next, let's translate the above information into code that creates a Car class.

Java Object Example

To create an object instance, you need to target the class it will belong to; for the sake of clarity, let's start with creating a custom class for the car concept above. Before we dive into the code sheck out this video on five different ways to create an object in Java.

To create this class, you would need the following code:

public class Car{    
    private String vinNum;
    private boolean isRunning;
    private int speed;
    // construct a new student 
    public Car(String vinNum) {
        this.vinNum = vinNum;
    public void setIsRunning() {
     this.isRunning = !isRunning;
    public boolean getIsRunning() {
        return isRunning;
    public void setSpeed(int speed) {
        this.speed = speed;
    public int getSpeed() {
        return speed;

Above is a car class with all needed to create a basic car object with the minimum characteristics to function. From here, all that is required is to import this into your main file and create an object instance of a car to which you can assign values. Let’s look at the code you need to create a car object instance in your main class.

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car("vhjujhm16513hgtfs5rf82f");
        myCar.speed = 35;
        String carVin = "The vin for this car is: " + myCar.vinNum;
        String carState = "The car’s running state is: " + myCar.isRunning;
        String carSpeed = "Current speed: " + myCar.speed;

The above code creates the main class; assuming you’ve imported the, you have access to the Car class within your main file. From this point, creating an object instance only requires a single line of code, which you can see below.

Car myCar = new Car("vhjujhm16513hgtfs5rf82f");

This simple line of code creates a new object instance and initiates it with a VIN, which serves as its unique identifier. From this point, you have the object and can start accessing its information, such as changing its running state and speed.

You can see a more advanced code version over on this codepen. While codepen doesn’t support Java code, you can copy the code and try it out in your own Java environment.

Getting Started With Java Objects

With all the above information, you have everything you need to start using Java object instances in your programs. You even have a simple class and object instance declaration to begin practicing with them in your code. Furthermore, you have access to a more advanced version of the code in this post which includes a couple of methods you can practice using with your object.

The learning doesn’t stop there; you can also utilize online resources, the Java community, and much more. Combining these resources into your practice work can quickly build your understanding and discover the power behind Java objects.



Topics: Java

Related Articles

We're committed to your privacy. HubSpot uses the information you provide to us to contact you about our relevant content, products, and services. You may unsubscribe from these communications at any time. For more information, check out our Privacy Policy.

Learn more about one of the world's most popular programming languages.