<< Chapter < Page Chapter >> Page >
class Worker{ private int myInt = 100;private double myDouble = 222.0; private boolean myBoolean;public Worker(){myInt = 20; }//end noarg constructor//...

Using hard-coded values for initialization

This fragment illustrates the third of the four ways listed earlier to establish the initial value of the instance variables of an object of the classnamed Worker . In particular, this fragment assigns the hard-coded value 20 to the instance variable named myInt , thus overwriting the value of 100 previously established for that variable by an initializationexpression.

(All objects instantiated from the Worker class using this noarg constructor would have the same initial value for the variablenamed myInt .)

Note, that this constructor does not disturb the initial values of the other two instance variables that were earlier established by an initializationexpression, or by taking on the default value. Thus, the initial values of these two instance variables remain as they were immediately following the declarationof the variables.

Initial values using this noarg constructor

When an object of the Worker class is instantiated using this constructor and the values of the three instance variables are displayed,the results are as shown below:

20 222.0 false

The value of myInt is 20 as established by the constructor. The value of myDouble is 222.0 as established by the initialization expression, and the value of myBoolean is false as established by default.

Using constructor parameters for initialization

The next fragment shows the last of the four ways listed earlier for establishing the initial value of an instance variable.

public class Ap119{ public static void main(String args[]){//... new Worker(5,true).display();//... }//end main()}//end class Ap119 class Worker{private int myInt = 100; private double myDouble = 222.0;private boolean myBoolean; //...public Worker(int x, boolean y){myInt = x; myBoolean = y;}//end parameterized constructor//...

A parameterized constructor

The above fragment shows the second of two overloaded constructors for the class named Worker . This constructor uses two incoming parameter values to establish the values of two of the instance variables,overwriting whatever values may earlier have been established for those variables.

The above fragment uses this constructor to instantiate an object of the Worker class, assigning incoming parameter values of 5 and true to the instance variables named myInt and myBoolean respectively. This overwrites the value previously placed in the variable named myInt by the initialization expression. It also overwrites the default value previouslyplaced in the instance variable named myBoolean .

(Note that this constructor doesn't disturb the value for the instance variable named myDouble that was previously established through the use of an initialization expression.)

Initial values using parameterized constructor

After instantiating the new object, this fragment causes the values of all three instance variables to be displayed. The result is:

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