<< Chapter < Page Chapter >> Page >
These learning objects explain constructors, which are used to create and initialize objects in Java.

Learning objects for constructors

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.

What are constructors for?

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.

  • The variable song1 is allocated and contains the null value.
  • Memory is allocated for the fields of the object; this is displayed in the Instance and Array Area. Default values are assigned to the three fields.
  • The default constructor is called but does nothing except return a reference to the object.
  • The reference is stored in the variable song1 .
  • The reference in song1 is used to assign values to the fields of the object.
  • The reference in 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).

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Learning objects for java (with jeliot). OpenStax CNX. Dec 28, 2009 Download for free at http://cnx.org/content/col10915/1.2
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Learning objects for java (with jeliot)' conversation and receive update notifications?

Ask