Here's a nice in-depth 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 Object
s. 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 HumanBeing
s. 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.
HumanBeingProgram.java
package com.howtoprogramwithjava.constructors;
public class HumanBeingProgram extends Animal
{
public static void main (String[] args)
{
HumanBeing me = new HumanBeing();
output(me);
HumanBeing you = new HumanBeing("blue", "female", "Jane Doe");
output(you);
}
private static void output(HumanBeing human)
{
System.out.println(human.getName() + "'s eyes are: " + human.getEyeColor());
System.out.println(human.getName() + " is " + human.getSex());
System.out.println("--------------");
}
}
HumanBeing.java
package com.howtoprogramwithjava.constructors;
public class HumanBeing extends Animal
{
String name;
public HumanBeing ()
{
super();
this.name = "John Doe";
}
public HumanBeing (String eyeColor, String sex)
{
super(eyeColor, sex);
}
public HumanBeing (String eyeColor, String sex, String name)
{
super(eyeColor, sex);
this.name = name;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
Animal.java
package com.howtoprogramwithjava.constructors;
public abstract class Animal
{
String eyeColor;
String sex;
public Animal ()
{
this.eyeColor = "brown";
this.sex = "male";
}
public Animal (String eyeColor, String sex)
{
this.eyeColor = eyeColor;
this.sex = sex;
}
public String getEyeColor()
{
return eyeColor;
}
public void setEyeColor(String eyeColor)
{
this.eyeColor = eyeColor;
}
public String getSex()
{
return sex;
}
public void setSex(String sex)
{
this.sex = sex;
}
}
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";
}
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();
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";
}
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";
}
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://www.coderscampus.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 :)