Java Objects and how are they created

20th February 2019 0 By Alin Bistrian

Before we get into what are Java Objects and how are they created let us have a look into what is Java.

 

Java is an Object Oriented Programming language which is made out of a collection of objects that are talking to each other.

 

And before we get into any syntax and how are we going to write Java code we need to understand what we want to accomplish?

 

For instance, if we’re going to create a desktop game for a race car simulation, we may want to have an object for Ferrari cars, one for Mercedes cars and one for the Race Drivers.

 

Having to create objects is one of the parts I like about Java and Object-Oriented Programming, and you may ask yourself why?

 

Because now we switched focus from “Oh I need to create a complicated race car game”  to  “I have a list of objects that I need to create, and I can focus on one at a time.”

 

We have broken down our big task of creating the game to create an object. Your work will be more focused, and every time you finish building an object you have made progress that you can track which will boost your motivation and confidence.

 

Ok, now we can reduce the enormous task of learning Java to learn about a Java Object. To do that we need to answer a couple of questions:

  • What are Java Objects and why do we need them?
  • How can we create Objects in Java?

 

What are Java Objects and why do we need them?

Looking into the real-life world, we can see a lot of objects around us which we may want to represent with Java when we are creating our software. For example cars, humans, race car drivers, dogs, etc.

 

You may argue here that, oh wait, humans, dogs are not objects so what are you talking about? Yes, they are not objects, but Java Objects can represent them.

All these objects have one thing in common, and that is they all have a state and behavior.

  • The state is what that object knows about itself
  • Behavior is what that object can do

 

Let us consider these two object examples: dog and Car

Dog Object:

  • Name, breed, and color represent the state.
  • Barking, playing, running represent the behavior of a dog object.

 

Car Object:

  • make, model, color, number of wheels represents the state
  • accelerating, braking constitutes the object behavior.

 

Now let’s go back to our race car simulator game and pick an example. For instance, we want to create a Ferrari Car Object. What will be the state and behavior of this object?

 

Ferarri Car Object’s state:

  • make = Ferarri
  • model = GTC4LUSSOT
  • color = red
  • number of wheels = 4

 

Ferarri Car Object’s behavior:

  • accelerate – (the object can accelerate if asked to do so)
  • break -(the object can break if asked to do so)

 

When we said that Java is a collection of objects, these are the kind of objects we were talking about.

 

What about talking to each other part we have mentioned? To demonstrate how and more important why objects need to communicate with each other, let us define another object called “Driver.”

 

What would represent the state of our Driver?

  • name, height, the weight would be the state
  • driving will be the behavior

 

Now it is obvious why the Driver Object needs to communicate with the Car Object because it needs to accelerate, brake, steer the wheel, etc. Now here it comes the fun part.

 

The Driver Object knows that when the acceleration pedal is pressed the Car Object accelerates, but how the car accelerates internally it is all up to the Car Object.

 

The conclusion is that each object should have it’s own focused purpose and needs to have a clear and easy way of communicating to other objects.

 

How are the Objects created in Java?

In Java, the JVM (Java Virtual Machine) will create the objects, and you will have to provide instructions on how those are created.

 

The way to provide those instructions is by writing Java Code. The place where all the instructions for a particular object creation go is something called a java class where its name represents the object type.

 

Here is how such a class looks like:


public class Car {

    String make = "Ferarri";
    String model = "GTC4LUSSOT";
    String color = "red";
    int numberOfWheels = 4;

    public void accelerate() {
        //Accelerate code here
    }

    public void breaking() {
        //Breaking code here
    }
}

 

The above code is how object creation instructions look like. And now we want to see how an object is created using these instructions.

 

Who is more likely to use a Car object? Well, a Driver object. Let’s create it.


public class Driver {

    String driverName = "Steve";
    int heightInCm = 180;
    int weightInKg = 70;

    public void drive() {
        //Here the driver needs a Car Object to drive it
        //And this is the way a car object is created from the instructions above
        Car car = new Car();
        car.accelerate();
    }
}

 

An object is created by the JVM when it encounters the word “new” followed by the type of object that needs to build. In our examples, a new Car object is created.

 

After that, the Driver object can communicate with the newly created Car Object by invoking its methods. In this case, our driver accelerates. We will discuss the syntax in the next tutorial so do not focus on it for the moment.

 

If there is one thing that I want you to remember from this tutorial is that all the code you will write in Java would be to write code into these classes which means you will write instructions for the JVM to create your objects.

 

A few things Java Objects can do are:

  • Hold data about itself.
  • Create new objects and communicate with those newly created objects.
  • Hold information about other objects.

 

In the next tutorial, we will have a closer look at these classes, and we will explain all the syntax used. Stay tuned.