Understanding the Java Constructor

Introduction

All Java objects have to be created at some point. These objects use a Java constructor to create themselves because constructors tell Java how to create the object. C++ and other object oriented languages also have the concept of constructors, so for those who know what they are, this will be an overview. In order to make the most of your objects however, you'll need to know how to create these constructors.

The Default Java Constructor

As has been discussed before, all Java objects have a constructor, even if you don't specifically code it. The same as not writing a constructor at all would be this:

Inside of that class is the default constructor. Again, constructors must be the name of the class, and the constructor must not have a return type at all.

What Constructors Do

Constructors may contain code that is run when the object is created. It is sort of like setup code that you want done so the object is ready for what it's supposed to do.

Here's an example of what I'm talking about:

You no longer HAVE to create an object and then fill the initial data with setters. You can skip the setters initially by using the constructor to set your data up. This doesn't mean setters are useless, because your data can change. However, this is just a way of making sure you don't create objects such as dogs initially with no legs. That's just an example of course.

Overloading Java Constructors

Constructors in Java may be overloaded. This means that a class can have MORE than one constructor. This is useful for when you want the object to be created with different parameters up front. There are two ways a Java constructor can be overwritten: give the new constructor a different number of parameters, or give the new constructor at least one different type of parameter.

Here's an example of overloading the default constructor:

Java can still tell the difference between all of those different constructors because the data that has to be given to them are all different, either by amount or by type. You'll also notice that all of the constructors set their data in slightly different ways. In general though, I prefer to call the setters from inside the constructor, like in the 3rd constructor in the example above. This way you're not writing duplicate code.

If you couldn't tell, this was part 3 of the Java objects tutorial. You now have all the information you need to create sophisticated objects.

Remember, objects do not have to represent real life objects that you can touch. Ideas can be objects too. You can create objects that contain a bunch of math formulas, or objects that only store data and don't do anything else. It is up to you to decide what objects your programs need and it is up to you to decide how to use them.

Objects are the biggest component of object oriented programming (it's called object oriented for a reason), and I highly recommend mastering the whole process of creating a Java object. It's critical to know for just about every object oriented language I can think of.

In the next tutorial we'll use Java packages to organize our programs when they become seemingly too large to manage. Happy coding!

If you have any questions, comments, or concerns about this tutorial, feel free to contact us.

Custom Search