<< 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

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 to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
characteristics of micro business
for teaching engĺish at school how nano technology help us
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
is Bucky paper clear?
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
Yeah, it is a pain to say the least. You basically have to heat the substarte up to around 1000 degrees celcius then pass phosphene gas over top of it, which is explosive and toxic by the way, under very low pressure.
Do you know which machine is used to that process?
how to fabricate graphene ink ?
for screen printed electrodes ?
What is lattice structure?
s. Reply
of graphene you mean?
or in general
in general
Graphene has a hexagonal structure
On having this app for quite a bit time, Haven't realised there's a chat room in it.
what is biological synthesis of nanoparticles
Sanket Reply
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?