0% found this document useful (0 votes)
57 views

5.1 Java - Lecture71 PDF

Uploaded by

tedyoung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

5.1 Java - Lecture71 PDF

Uploaded by

tedyoung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Lesson 71

Creating and Using


Constructors

PRESENTED BY

Keith Vassallo icemalta.com


An Object’s Lifecycle
An object is instantiated and then initialised.

Dog rex = new Dog();

icemalta.com
Constructors
- We can create a special ‘method’ called a constructor.
- The constructor will run automatically when a new instance of a class (i.e.
an object) is created.
- We can use constructors to set initial values.
- Even when we don’t specify a constructor, all classes have an invisible
constructor, that doesn’t really do anything.
public class Dog {
String breed;
int age;
String name; Since we didn’t add our own
public Dog() { }
constructor in this class, Java uses

icemalta.com
this default, invisible constructor, to
void sleep() { allow the class to be initialised.
System.out.println("Dog is sleeping");
}
}
Constructors
We can add our own constructor that does something. Let’s start with
something simple.
public class Dog { public static void main(String[] args) {
String breed; Dog rex = new Dog();
int age; }
String name;

public Dog() {
System.out.println("New Dog instance created");
}
}

- Note how we don’t call the constructor – it runs automatically when the

icemalta.com
instance is created.
- A constructor must have the same name as the class, and has no return
type (not even void).
Constructors
- Let’s make our constructor useful. It will accept the age and breed of a
Dog as parameters, and will set the fields to these values.
public class Dog { public static void main(String[] args) {
String breed; Dog rex = new Dog(“Labrador”, 5);
int age; }
String name;

public Dog(String dogBreed, int dogAge) {


System.out.println("New Dog instance created");
breed = dogBreed;
age = dogAge;
}
}

icemalta.com
- Now we can create a new Dog instance and immediately specify the
breed and age of the dog.
Caveat
- Unfortunately, this means that we now must specify the breed and age of
a Dog whenever we create one.
public class Dog { public static void main(String[] args) {
String breed; Dog rex = new Dog(“Labrador”, 5);
int age; Dog fido = new Dog();
String name; }

public Dog(String dogBreed, int dogAge) {


System.out.println("New Dog instance created");
breed = dogBreed;
age = dogAge; This won’t work now, since there’s
} no constructor that doesn’t accept
} any argument.

icemalta.com
Caveat Solution
To solve this, we can overload the constructor, so two versions are available.
public class Dog { public static void main(String[] args) {
String breed; Dog rex = new Dog(“Labrador”, 5);
int age; Dog fido = new Dog();
String name; }

public Dog() {
System.out.println("New Dog instance created");
}
Java now decides which
public Dog(String dogBreed, int dogAge) { constructor to use.
System.out.println("New Dog instance created");
breed = dogBreed;

icemalta.com
age = dogAge;
}
}
Overloaded Constructor
We can have as many constructors as we need.
public class Dog { public static void main(String[] args) {
String breed; Dog fido = new Dog();
int age; Dog rex = new Dog(“Labrador”, 5);
String name; Dog fred = new Dog(“Pug”, 4, ”Fred”);
}
public Dog() {
System.out.println("New Dog instance created");
}

public Dog(String dogBreed, int dogAge) {


System.out.println("New Dog instance created");
breed = dogBreed;
age = dogAge;
}

public Dog(String dogBreed, int dogAge, String dogName) {


System.out.println("New Dog instance created");

icemalta.com
breed = dogBreed;
age = dogAge;
name = dogName;
}
}
Great work, you’ve completed this lesson!

Next up: Using the ‘this’ and ‘super’


keywords.

icemalta.com
© 2011-2017 Institute of Computer Education Ltd.

The contents of this document are copyright to the Institute of Computer Education Ltd, unless otherwise stated, and must not be reproduced without permission.

Every effort has been made to trace all of the copyright holders, but if any have been inadvertently overlooked the Institute of Computer Education Ltd. will be pleased to make the necessary arrangements at the first
opportunity. Please contact us directly. While the Institute of Computer Education Ltd. has taken all reasonable care in the preparation of this work, the Institute of Computer Education Ltd. makes no representation,
express or implied, with regard to the accuracy of the information contained in this work and cannot accept any legal responsibility or liability for any errors or omissions from the work or the consequences thereof.
The reader assumes sole responsibility for the selection of these materials to achieve its intended results. Products and services that are referred to in this work may be either trademarks and/or registered
trademarks of their respective owners. The editors and author/s make no claim to these trademarks. icemalta.com

You might also like