<< Chapter < Page Chapter >> Page >

5 222.0 true

As you can see, the values contained in the instance variables named myInt and myBoolean are the values of 5 and true placed there by the constructor, based on incoming parameter values. The value in the instance variable named myDouble is the value placed there by the initialization expression when the variable was declared.

Default initialization

If you don't take any steps to initialize instance variables, they will be automatically initialized. Numeric instance variables will be initialized withzero value for the type of variable involved. Instance variables of type boolean will be initialized to false. Instance variables of type char will be initialized to a Unicode value with all 16 bits set to zero. Reference variableswill be initialized to null.

Initialization expression

If you provide an initialization expression for an instance variable, the value of the expression will overwrite the default value, and the value of theinitialization expression will become the initial value for the instance variable.

Assignment in constructor code

If you use an assignment statement in a constructor to assign a value to an instance variable, that value will overwrite the value previously placed in theinstance variable either by default, or by use of an initialization expression. The constructor has the "last word" on the matter of initialization of instancevariables.

Back to Question 10

Answer 9

A. Compiler Error

Explanation 9

The default constructor

When you define a class, you are not required to define a constructor for the class. If you do not define a constructor for the class, a default constructorthat takes no arguments will be provided on your behalf. You can instantiate new objects of the class by applying the new operator to the default constructor asshown in the following code fragment from Question 8 .

new Worker().display();

Behavior of the default constructor

As illustrated in Question 8 , when you don't provide a constructor that purposely initializes the values of instance variables, or initialize them insome other manner, they will automatically be initialized to the default values described in Question 8 .

Defining overloaded constructors

You can also define one or more overloaded constructors having different formal argument lists. The typical intended purpose of such constructors is touse incoming parameter values to initialize the values of instance variables in the new object.

A parameterized constructor

This is illustrated in the following code fragment. This fragment receives two incoming parameters and uses the values of those two parameters toinitialize the values of two instance variables belonging to the new object.

class Worker{ private int myInt;private boolean myBoolean; public Worker(int x, boolean y){myInt = x; myBoolean = y;}//end parameterized constructor//display() omitted for brevity}// end class Worker

If you define any constructors ...

However, there is a pitfall that you must never forget.

If you define any constructors in your new class, you must define all constructors that will ever be required for your new class.

Questions & Answers

if three forces F1.f2 .f3 act at a point on a Cartesian plane in the daigram .....so if the question says write down the x and y components ..... I really don't understand
Syamthanda Reply
hey , can you please explain oxidation reaction & redox ?
Boitumelo Reply
hey , can you please explain oxidation reaction and redox ?
Boitumelo
for grade 12 or grade 11?
Sibulele
the value of V1 and V2
Tumelo Reply
advantages of electrons in a circuit
Rethabile Reply
we're do you find electromagnetism past papers
Ntombifuthi
what a normal force
Tholulwazi Reply
it is the force or component of the force that the surface exert on an object incontact with it and which acts perpendicular to the surface
Sihle
what is physics?
Petrus Reply
what is the half reaction of Potassium and chlorine
Anna Reply
how to calculate coefficient of static friction
Lisa Reply
how to calculate static friction
Lisa
How to calculate a current
Tumelo
how to calculate the magnitude of horizontal component of the applied force
Mogano
How to calculate force
Monambi
a structure of a thermocouple used to measure inner temperature
Anna Reply
a fixed gas of a mass is held at standard pressure temperature of 15 degrees Celsius .Calculate the temperature of the gas in Celsius if the pressure is changed to 2×10 to the power 4
Amahle Reply
How is energy being used in bonding?
Raymond Reply
what is acceleration
Syamthanda Reply
a rate of change in velocity of an object whith respect to time
Khuthadzo
how can we find the moment of torque of a circular object
Kidist
Acceleration is a rate of change in velocity.
Justice
t =r×f
Khuthadzo
how to calculate tension by substitution
Precious Reply
hi
Shongi
hi
Leago
use fnet method. how many obects are being calculated ?
Khuthadzo
khuthadzo hii
Hulisani
how to calculate acceleration and tension force
Lungile Reply
you use Fnet equals ma , newtoms second law formula
Masego
please help me with vectors in two dimensions
Mulaudzi Reply
how to calculate normal force
Mulaudzi
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, 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