<< Chapter < Page | Chapter >> Page > |
Concept The process of creating an object involves allocating memory for the object and assigning the reference to this block of memory to a variable. Constructors enable arbitrary initialization of the object during its creation.
These source code of these learning objects can be found in constructor.zip .
LO | Topic | Java Files (.java) | Prerequisites |
"What are constructors for?" | What are constructors for? | Constructor01A, B, C | |
"Computation within constructors" | Computation within constructors | Constructor02 | 1 |
"Overloading constructors" | Overloading constructors | Constructor03 | 2 |
"Invoking an overloaded constructor from within a constructor" | Invoking another constructor | Constructor04 | 3 |
"Explicit default constructors" | Explicit default constructors | Constructor05 | 3 |
"Constructors for subclasses" | Constructors for subclasses | Constructor06A, B, C | 3 |
"Constructors with object parameters" | Constructors with object parameters | Constructor07 | 3 |
"Constructors with subclass object parameters" | Constructors with subclass | ||
object parameters | Constructor08 | 6, 7 |
Program The example used in these LOs is class
Song
with three fields: the
name
of the song, the length of the song
in
seconds
and the
pricePerSecond
. The class is to be
used to implement a website which charges for downloading the song; theprice is the product of the length of the song in second and the price
per second. To focus the discussion on constructors, the fields are notdeclared private.
Concept An object is created by allocating memory for its fields. The fields are given the default values for their types. A reference to the object is returned and assigned to a variable; the reference can be used to access the fields and methods of the object.
Program: Constructor01B.java
// Learning Object Constructor01A
// what are constructors for?class Song {
String name; int seconds;
double pricePerSecond;
public double computePrice() { return seconds * pricePerSecond;
}}
public class Constructor01A {
public static void main(/*String[] args*/) {
Song song1 = new Song(); song1.name = "Waterloo";
song1.seconds = 164; song1.pricePerSecond = 0.01;
double price = song1.computePrice(); }
}
If a constructor is not explicitly declared a default constructor is called.
song1
is allocated and contains the null value.song1
.song1
is used to assign values to the fields of the object.song1
is used to call the method
computePrice
on the object; the method computes and returns the price,
which is assigned to the variable
price
.Concept An explicit constructor method can be declared and used to initialize each object.The constructor method is identified by a special syntax: the name of the method is the same as the name of the class and there is no return type (because the value returned is of the type of the class itself).
Notification Switch
Would you like to follow the 'Learning objects for java (with jeliot)' conversation and receive update notifications?