Constructors Java Tutorial

Transcript of Constructor Java Tutorial

Hi everyone and welcome back to the how to program with java blog, my name is Trevor Page, and today I’ll be talking a little bit about Constructors in Java.

So, what are Constructors? Why are they important? and what do they look like in Code? Let’s find out!

So, what are they? A constructor is really the building blocks of an Object. It’s the first thing that Java will execute when a new Object is instantiated. So if you were to instantiate a new Object, Java will go to that Object‘s constructor code and execute whatever you had placed in that code. Make sense? Pretty straightforward.

Why are Constructors important?

Well, it allows us to really initialize our Objects. It puts them into a default/known state, this is really important when you have an Object that should be in a default state. You don’t want to have variables that are left as NULL. You want to immediately those variables in a known state in that Object, so you won’t have any errors or strange behaviour down the line.

What do Constructors look like in Code?

Let’s find out. I’ve put together a little example that revolves around Animals and HumanBeings. It uses a little bit of Inheritance – a HumanBeing is an Anmial. We talked about this back in the Inheritance post, and the “is a” relationship.

As you can see here, a constructor looks like a method, but it’s missing one key part that defines a method. The return type! A constructor looks just like a method with no return type, but the constructor is named after the Class it belongs to. If we even change the “case” of a constructor’s name, we’ll get an error. So for example, if we change the Animal Class’ constructor to say:

public animal (){this.eyeColor="brown";this.sex="male";}

public animal ()
{
this.eyeColor = "brown";
this.sex = "male";
}

Then Java will complain and say “the return type is missing”, because it thinks you’re trying to create a method. Since we’re not creating a method, we’re creating a constructor, so it has to be exactly the same name as the Class that defines it.

You can have multiple constructors in a Java class, and this is accomplished by using different parameters in your constructors.

Now, when you instantiate a Class, Java will execute the constructor that you specify – which is determined by the parameters that you pass in, so if you were to execute this code:

HumanBeing me =new HumanBeing();

HumanBeing me = new HumanBeing();

Java will execute the constructor for HumanBeing that does not have any parameters/arguments. So this would execute the following code:

public HumanBeing (){super();this.name="John Doe";}

public HumanBeing ()
{
super();
this.name = "John Doe";
}

Now, there’s some new code there. That super keyword.

What the heck does super mean? All this means is that we want to call the constructor that is one step up in our inheritance chain. This means that Java will call the Animal constructor. Now, which one will it call? It will call the constructor that takes NO parameters/arguments, because we have not specified any paramters in our super() code! Here’s the code it’ll end up executing:

public Animal (){this.eyeColor="brown";this.sex="male";}

public Animal ()
{
this.eyeColor = "brown";
this.sex = "male";
}

So, when we run this Java application, what is the output that we should expect to see? Well here’s the output that this program creates:

John Doe’s eyes are brown
John Doe is male
——————
Jane Doe’s eyes are blue
Jane Doe is female

So, does that make sense to you guys? Constructors are fairly straight-forward and this is pretty much as complicated as I can think to throw at you with the super() keyword and having multiple constructors with different parameters. Normally you just see constructors with no parameters that just initialize some variables, but you’ll see a bunch of different examples of constructors as you go through your Java training or career as a Java programmer!

Alright, so thank you very much for sticking with us until the end of this video tutorial, I have one favour to ask of you. If you could go to https://howtoprogramwithjava.com/signup and either sign-up for my mailing list, follow me on twitter, like my facebook fan-page (or all three), I would very much appreciate it! And as always, if you have any questions, please post them in the comments section below 🙂

Hi Trevor. Great blog. I found your site through Lifehacker and I’ve been working my way through the tutorials. I noticed your company name at the end of the video…I googled it and found out that it’s near my house. I’m a CS student at uWaterloo. I’m learning OOP in C++ right now, so I figured I’d try and learn Java at the same time.

I have a quick question. It’s not actually about Java but about how you post code to your blog. I have a WordPress blog too, but haven’t found a good solution for embedding code. I’ve been using Pastebin, but I end up with errors if I go back and edit posts. What do you use?