<< Chapter < Page Chapter >> Page >
  • The value of the static constant DEFAULT_PRICE is set as soon as the class is loaded and is displayed in the Constant area.
  • The variable song1 is allocated and contains the null value.
  • Memory is allocated for the four fields of the object and default values are assigned to the fields.
  • The constructor is called with three actual parameters; the call is resolved so that the first constructor is executed. These values are assigned to the formal parameters of the constructor method and the values of the formal parameters are assigned to the three fields.
  • The method computePrice is called; it returns a value which stored in the field price .
  • The constructor returns a reference to the object, which is stored in the variable song1 .
  • The computation is then repeated for song2 . Since the constructor is called with just two parameters (for name and seconds ), the second constructor is executed.The value of the field pricePerSecond is assigned from the constant, not from a parameter.

Exercise Modify the class to include a constructor with one parameter for the name and with a default song length of three minutes.

Exercise Modify the class to include a constructor with no parameters, so that all fields receive default values. Is there any meaning to the following constructor?

Song() { }

Invoking an overloaded constructor from within a constructor

Concept Constructors can be overloaded like other methods. A method is overloaded when there is more than one method with the same name; the parameter signature is used to decide which method to call. For constructors, overloading is usually done when some of the fields of an object can be initialized with default values, although we want to retain the possibility of explicitly supplying all the initial values. In such cases, it is convenient to invoke one constructor from within another in order to avoid duplicating code. Invoking the method this within one constructor calls another constructor with the appropriate parameter signature.

Program: Constructor04.java

// Learning Object Constructor04 //    invoking one constructor from anotherclass Song {     String name;    int seconds;     double pricePerSecond;    double price;     final static double DEFAULT_PRICE = 0.005;      Song(String n, int s, double p) {        name = n;         seconds = s;        pricePerSecond = p;         price = computePrice();    }      Song(String n, int s) {         this(n, s, DEFAULT_PRICE);    }      private double computePrice() {         return seconds * pricePerSecond;    } }  public class Constructor04 {    public static void main(/*String[] args*/) {        Song song1 = new Song("Waterloo", 164);     }}

The website charges a uniform price per second for all songs, except for special offers. We define two constructors, one that specifies a price for special offers and another that uses a default price for ordinary songs.

  • The value of the static constant DEFAULT_PRICE is set as soon as the class is loaded and is displayed in the Constant area.
  • The variable song1 is allocated and contains the null value.
  • Memory is allocated for the four fields of the object and default values are assigned to the fields.
  • The constructor is called with two actual parameters; the call is resolved so that it is the second constructor that is executed.
  • The two parameters, together with the default price, are immediately used to call the first constructor that has three parameters. The method name this means: call a constructor from this class. This constructor initializes the first three fields from the parameters, and the value of the fourth field is computed by calling the method computePrice .
  • The constructor returns a reference to the object, which is stored in the variable song1 .

Questions & Answers

what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
Bharti
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
Daniel
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
Maciej
characteristics of micro business
Abigail
for teaching engĺish at school how nano technology help us
Anassong
Do somebody tell me a best nano engineering book for beginners?
s. Reply
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
s.
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.
Tarell
what is the actual application of fullerenes nowadays?
Damian
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.
Tarell
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.
Virgil
is Bucky paper clear?
CYNTHIA
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.
Harper
Do you know which machine is used to that process?
s.
how to fabricate graphene ink ?
SUYASH Reply
for screen printed electrodes ?
SUYASH
What is lattice structure?
s. Reply
of graphene you mean?
Ebrahim
or in general
Ebrahim
in general
s.
Graphene has a hexagonal structure
tahir
On having this app for quite a bit time, Haven't realised there's a chat room in it.
Cied
what is biological synthesis of nanoparticles
Sanket Reply
what's the easiest and fastest way to the synthesize AgNP?
Damian Reply
China
Cied
types of nano material
abeetha Reply
I start with an easy one. carbon nanotubes woven into a long filament like a string
Porter
many many of nanotubes
Porter
what is the k.e before it land
Yasmin
what is the function of carbon nanotubes?
Cesar
I'm interested in nanotube
Uday
what is nanomaterials​ and their applications of sensors.
Ramkumar Reply
what is nano technology
Sravani Reply
what is system testing?
AMJAD
preparation of nanomaterial
Victor 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
Good
Got questions? Join the online conversation and get instant answers!
QuizOver.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?

Ask