OBJECT CREATION IN JAVA
By: Nonso Biose
Tags: Java, Learning To Code, Programming, Software Development, Object Oriented

Welcome, back again, if you have been constantly keeping up with this Java series, then I want to say a BIG thank you to you for taking time to read through my efforts and if this is your first time, then welcome on board. This blog series is not like any other type that you may have encountered, its different in the sense that you don’t come here to learn what you already know(“we don’t show you the codes you already know”) instead we build upon that not too solid knowledge. This is done through the use of some graphical images, lay-man terms and snippet-like codes that all help to simplify your understanding of the concepts in Java.

Now moving on to the topic of today, as we all know that the programming approach utilized by Java is known as an object oriented programming approach. This basically models real life objects in programming as they would be in real life. This also involves the various objects having their state and behavior. They can also interact with each other e.g , a light bulb switch which is an object interacts with the light bulb object to bring about the lighting of the bulb. Just as the analogy of the light bulb and light bulb switch is, so they can also be in a Java application.

Note: everything in Java, is an object.

Object creation in java starts with some file called a class which is basically where all our Java codes go into(“You cannot program in java outside a class”). The next question is obviously what is a class in Java????

In java, a class is not anything like what any novice programmer thinks it is, instead it is simply the blueprint of an object.

Image from dictionary.com

According to the dictionary.com definition of a blueprint, you can see that its is a detailed outline of what an object in java or any other object would look or act like…. So what is an object as it relates to Java???

An object in Java just like it would be in real life is anything with a state and can do anything or anything can be done to it. So basically it is safe to say that in java if everything we write must be in class, then objects in Java can only come from those classes (“objects in java are defined by the classes in which they come from”).

Image gotten from google

So going deeper, it is a tradition here on this blog series that the readers of this Java post is currently into active coding. If so, then you have started working with several classes in Java and must or may have created some objects out of those classes, if not so, then here is a example of what a class looks like in Java.

List of classes that can be created in Java

So the above pic is what a class in Java will look like when you create one and inside the class goes everything that objects from such class will have including its methods. Also a class is identified by its name which is the word just before the curly braces(“when naming classes, it is of best practice to start the word with a Capital letter and it should reflect exactly what the objects of the class would be and do”)

Note: Objects in Java are created out of classes. Also objects created from a class is known as an instance of that class and also, several objects can be created from one particular class… we shall soon see this later on in this topic.

Looking at the above pic, the name of the class is obvious(“Car”) and you can see some strange things in it. The first four lines inside the curly braces in the class Car are known as class fields.(“they are also member variables of the class Car but are generally called class fields”). Now what they do is basically describe the state of the objects to be created from it.When we talk state, we mean the nature of the objects which as you can see above are the model, color, number of gears and also transmission of the car(“objects of class car”).

Note: Every object whether in Java or real life, must have a state. You as a human, you also have a state e.g Two legs, a nose, a skin color, height and many more…….

So when an object is created from this Car class, it will have those class fields as its state but that’s not all, after the declaration of the class fields, next is a method like stuff with a name that’s exact with that of the class….. why???

The method below the class fields is a special method in Java known as a Constructor….. What is a constructor. This is a special method in Java that initializes the class fields in a class with initial values upon the creation of the object. What i mean is that every object obviously has a state which could be its color, height, width, shape etc, but in the creation of such object, the creator of the object must apply the color, carve the shape and do many other things to the object for it to actually be the object that it is meant to be. As it is in real life, so it is in Java, the constructor method must fill in those empty class fields with various values that suit the description of the object to be created.

Looking at the above image again you notice that the constructor method(“if you don’t understand methods in java, CLICK HERE”) takes in parameters that are to be received by the class fields for initialization of their variables. On creation of the objects, this constructor method is called and as is the tradition in calling methods in Java, you must provide the constructor method with the adequate values that its parameters require else the object cannot be created i.e An instance of the class will not be created.

Also, you might notice that the constructor method appears twice in the class but with differences. Yes, that’s known as method overloading in Java(“CLICK HERE for a better understanding of method overloading in Java). The next novice question you might wanna ask is why would i want to overload a method in my Java class…. as said earlier, follow the link above….. But overloading a constructor method in Java, is done for the following reason.

Apart from the fact that when you want to create an object in java, you have to call its constructor and then fill in the requirements of its parameters(“the parameters of an object is basically the values that the object needs to have for it to be a valid object which can also be said to be the objects state”), also in Java you can also create an object with a constructor that takes in no parameter. Hmmmmm. Its a simple concept, go and look at the above image again. You will notice that the second constructor already has its fields initialized with values that describe the state of the classes objects. So when we create a constructor with no parameters, we must initialize the class fields with values….. see conversation below for futher explanation

Software Developer(Novice): keep on talking….. Software Developer(Pro): yeah! when you create a parametarized constructor in Java, you are implying that any programmer that intends to create objects out of your class, would have to feed in the aguments with the required values needed by the object. Software Developer(Novice): hmmmm….. ok??? Software Developer(Pro): Ya and also when you create an empty constructor, you as the programmer don't need to provide arguments. The implication is the fact that anybody creating objects out of your class, must use the objects in the state that they come in(“In this case, the programmer cannot modify the objects state when creating it”). Software Developer(Novice): wow !!!! Software Developer(Pro): so its up to you to decide whether to overload your constructor or just have a specific one.

Note: you can also have a constructor that is semi parametarized…. pic is below.