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

Biology is a branch of Natural science which deals/About living Organism.
Ahmedin Reply
what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
cell is the smallest unit of the humanity biologically
Abraham
what is biology
Victoria Reply
what is biology
Abraham
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
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