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

how do you get the 2/50
Abba Reply
number of sport play by 50 student construct discrete data
Aminu Reply
width of the frangebany leaves on how to write a introduction
Theresa Reply
Solve the mean of variance
Veronica Reply
Step 1: Find the mean. To find the mean, add up all the scores, then divide them by the number of scores. ... Step 2: Find each score's deviation from the mean. ... Step 3: Square each deviation from the mean. ... Step 4: Find the sum of squares. ... Step 5: Divide the sum of squares by n – 1 or N.
kenneth
what is error
Yakuba Reply
Is mistake done to something
Vutshila
Hy
anas
hy
What is the life teble
anas
hy
Jibrin
statistics is the analyzing of data
Tajudeen Reply
what is statics?
Zelalem Reply
how do you calculate mean
Gloria Reply
diveving the sum if all values
Shaynaynay
let A1,A2 and A3 events be independent,show that (A1)^c, (A2)^c and (A3)^c are independent?
Fisaye Reply
what is statistics
Akhisani Reply
data collected all over the world
Shaynaynay
construct a less than and more than table
Imad Reply
The sample of 16 students is taken. The average age in the sample was 22 years with astandard deviation of 6 years. Construct a 95% confidence interval for the age of the population.
Aschalew Reply
Bhartdarshan' is an internet-based travel agency wherein customer can see videos of the cities they plant to visit. The number of hits daily is a normally distributed random variable with a mean of 10,000 and a standard deviation of 2,400 a. what is the probability of getting more than 12,000 hits? b. what is the probability of getting fewer than 9,000 hits?
Akshay Reply
Bhartdarshan'is an internet-based travel agency wherein customer can see videos of the cities they plan to visit. The number of hits daily is a normally distributed random variable with a mean of 10,000 and a standard deviation of 2,400. a. What is the probability of getting more than 12,000 hits
Akshay
1
Bright
Sorry i want to learn more about this question
Bright
Someone help
Bright
a= 0.20233 b=0.3384
Sufiyan
a
Shaynaynay
How do I interpret level of significance?
Mohd Reply
It depends on your business problem or in Machine Learning you could use ROC- AUC cruve to decide the threshold value
Shivam
how skewness and kurtosis are used in statistics
Owen Reply
yes what is it
Taneeya
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