<< Chapter < Page Chapter >> Page >

Are you satisfied with default values?

As long as you are satisfied with the default initialization of all instance variables belonging to the object, there is no need for you to define aconstructor of your own.

However, in the event that you have initialization needs that are not satisfied by the default constructor, you can define your own constructor. Yournew constructor may or may not require arguments. (In case you have forgotten, the name of the constructor is always the same of the name of theclass in which it is defined.)

A non-default noarg constructor

If your new constructor doesn't require arguments, you may need to write code that performs initialization in ways that differ from the defaultinitialization. For example, you might decide that a particular double instance variable needs to be initialized with a random number each time a newobject is instantiated. You could do that with a constructor of your own designthat doesn't take arguments by defining the constructor to get initialization values from an object of the Random class.

A parameterized constructor

If your new constructor does take arguments, (a parameterized constructor) you can define as many overloaded versions as you need. Each overloaded version must have a formal argument list that differs from the formalargument list of all of the other overloaded constructors for that class.

(The rules governing the argument list for overloaded constructors are similar to the rules governing the argument list for overloaded methods, whichwere discussed in a previous module.)

Use parameter values for initialization

In this case, you will typically define your parameterized constructors to initialize some or all of the instance variables of the new object using valuespassed to the constructor as parameters.

What else can a constructor do?

You can also cause your new constructor to do other things if you so choose. For example, if you know how to do so, you could cause your constructor (with or without parameters) to play an audio clip each time a new object is instantiated. You could use a parameter to determine which audio clip to playin each particular instance.

The punch line

So far, everything that I have said is background information for this program. Here is the punch line insofar as this program is concerned.

If you define any constructor in your new class, you must define all constructors that your new class will ever need.

If you define any constructor, the default constructor is no longer provided on your behalf. If your new class needs a noarg constructor (and it probably does, but that may not become apparent until later when you or someone elseextends your class) you must define the noarg version in addition to the other overloaded versions that you define.

A violation of the rule

This program violated the rule given above. It defined the parameterized constructor for the class named NewClass shown below

public NewClass(int x){ this.x = x;}//end constructor

However, the program did not also define a noarg constructor for the NewClass class.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask