In the tutorial on the meaning of the static keyword, I introduced constructors briefly because I had to
show you one very good use of the static
keyword, which is counting every time a new object is created, and this can
be used to give the new objects their IDs. So in this tutorial, we delve deeper
into constructors.
A constructor is called whenever a new object is created.
Now, when you don’t define your own constructor, Java creates a default one for
you. However, if you define a custom constructor, Java will not create a
default one, but will use the one that you have provided to initialize your
objects.
So, you might be wondering, if Java can create a default
constructor for you, why go to all the trouble of defining your own
constructor? You see, you may want to do more when your object is created than
what the default Java constructor does. For example, let us look at how the
default constructor created by Java works. To keep things simple, we will use
the Person that we have been using
all along:
public class Person {
private int
age;
private
String name;
private int
id;
public
static void main(String[] args) {
Person
person1 = new Person();
person1.name
= “Brian”; // Always create getters & setters
person1.age
= 25;
person1.id
= 11111111;
}
}
In the above program, when we wrote Person person1 = new Person(); Java created
a default constructor for us because we hadn’t defined our own constructor
before the main function i.e. public static void main(String[] args){
Let me mention also that you can define more than one
constructor for your class, but with different functionality.
Structure of a Constructor
A constructor looks like this:
public
ClassName (parameters) {
statements;
}
public
ClassName (parameter) {
statements;
}
public
ClassName () {
}
Notice that a constructor is public because we would like to
access it from everywhere so long as we want to make an object of that class.
Then, notice that a constructor does not have a return type. It is just public ClassName (
The above code fragments show you three ways in which you can
create constructors. The first has several parameters and statements within its
body. The second has one parameter. To distinguish between these two, notice
that I am using parameters (plural) in the first and parameter (singular) in
the second. You can have parameters in your constructors whenever you want to
initialize your objects as they are created. The third constructor example
above has no parameters nor statements; but it is a constructor nonetheless.
So let’s say that I want to initialize all the member variables
in the Person class using a
constructor. Here is how I’d do it:
public class Person {
private String name;
private int age;
private int id;
public Person (String name, int
age, int id) {
this.name = name;
this.age = age;
this.id = id;
}
public static void main(String[]
args) {
Person
person1 = new Person(“Brian”, 25, 11111111);
System.out.println(person1.name);
System.out.println(person1.age);
System.out.println(person1.id);
}
}
Figure 1: Using a constructor to initiate all the member variables
of an object.
From the above example, you can see that I have defined
person1’s age, name and id all in one line: Person person1 = new Person(“Brian”, 25, 11111111);
Now, let’s say you don’t want to initialize all the member
variables of the class. Maybe you want to initialize only the name using the
constructor. Here is how you do it:
public class Person {
private
String name;
private int
age;
private int
id;
public
Person (String name) {
this.name
= name;
}
public
static void main(String[] args) {
Person
person1 = new Person (“Brian”);
person1.age
= 25;
person1.id
= 11111111;
System.out.println(person1.name);
System.out.println(person1.age);
System.out.println(person1.id);
}
}Figure 2: Using a constructor to initialize one of the member variables of a class’s objects.
As you can see, we have initialized person1’s name using a
constructor. We have not initialized the age and id member variables using the
constructor.
The final type of constructor has no parameters. It is
similar to what Java creates for us by default when we don’t define our own
constructors. It looks like this:
public class Person {
private
String name;
private int
age;
private int
id;
public
Person () {
}
public
static void main(String[] args) {
Person
person1 = new Person ();
person1.name
= “Brian”;
person1.age
= 25;
person1.id
= 11111111;
System.out.println(person1.name);
System.out.println(person1.age);
System.out.println(person1.id);
}
}
Figure 3: A constructor similar to the one Java creates for
you by default when you don’t define any constructor for your class.
Remember that Java only creates a default constructor for you
when you haven’t defined one for yourself. For example, let’s say that you
define a constructor that allows you to initialize all or some of the member
variables like this:
public class Person {
private
String name;
private int
age;
private int
id;
public
Person (String name) {
this.name
= name;
}
public
Person (String name, int age, int id) {
this.name
= name;
this.age
= age;
this.id
= id;
}
public
static void main(String[] args) {
Person
person1 = new Person (“Brian”, 25, 11111111);
Person
person2 = new Person (“Bob”);
person2.age
= 30;
person2.id
= 22222222;
Person person3 = new Person ();
//This will give an error.
System.out.println(person1.name);
System.out.println(person1.age);
System.out.println(person1.id);
}
}
Figure 4: When you define any constructor for your class,
Java no longer creates a default constructor for you anymore.
Notice that by defining even one custom constructor, Java
will no longer generate a default one for you. Therefore, this Person person3 = new Person ();
will generate an error and Java will complain that it cannot find the above
constructor. It will also suggest that you define it.
Therefore, whenever you define your constructors, always make
sure to define every possible constructor because you don’t know how someone
using your program will choose to initialize the member variables.
So in the Person class above, it would be a great idea to
have one constructor that initializes all the three variables. Also, create a
default constructor for those who choose to create the objects of the class
without having to initialize any of the member variables. Then, create setters
and getters just in case the person using your program decides to initialize
one of the member variables outside the constructor.
public class Person {
private
String name;
private int
age;
private int
id;
public
Person (String name, int age, int id) {
this.name
= name;
this.age
= age;
this.id
= id;
}
public Person () {
}
public void setName (String name) {
this.name
= name;
}
public void setAge (int age) {
this.age
= age;
}
public void setId (int id) {
this.id
= id;
}
public String getName() {
return
name;
}
public int getAge() {
return
age;
}
public int getId() {
return
id;
}
public
static void main(String[] args) {
Person
person1 = new Person (“Brian”, 25, 11111111);
Person
person2 = new Person ();
person2.setName(“Bob”);
person2.setAge
(30);
person2.setId
(22222222);
System.out.println(person1.name);
System.out.println(person1.age);
System.out.println(person1.id);
System.out.println();
//This just prints out a blank line.
System.out.println(person2.name);
System.out.println(person2.age);
System.out.println(person2.id);
}
}
Figure 5: You should use constructors to initialize your
member variables. Otherwise, always use setters and getters.
I know you might have noticed the keyword this in the above examples and in some
of the past tutorials. Just know that it refers to the class. So when I say this.name = name; what I mean is that
the name variable of the object being created (which belongs to this class) is equal to the value in the
parameter.
Based on the parameters you pass into your constructors; Java
will know which constructor to call.
I hope that this tutorial has shed light into the subject of
constructors. Practice a lot and create different variations of constructors
for your classes. You will learn more by reading and doing than you will learn
from just reading.
In case of any question or comment, please drop them in the
comments section below and I will make sure to answer them.
In the next tutorial, we learn how to request and get input
from the user.
Until
then, stay safe.
No comments:
Post a Comment