<< 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 variations in raman spectra for nanomaterials
Jyoti Reply
I only see partial conversation and what's the question here!
Crow Reply
what about nanotechnology for water purification
RAW Reply
please someone correct me if I'm wrong but I think one can use nanoparticles, specially silver nanoparticles for water treatment.
yes that's correct
I think
what is the stm
Brian Reply
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
How we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
scanning tunneling microscope
how nano science is used for hydrophobicity
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
what is differents between GO and RGO?
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
The nanotechnology is as new science, to scale nanometric
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

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?