<< Chapter < Page Chapter >> Page >

To summarize this situation, every time an instance method is called, it receives a hidden reference named this . That is a reference to the object on which the method was called.

The code in the method can use that reference to access any instance member of the object on which it was called, or any class member of the class from which the object was instantiated.

However, when class methods are called, they do not receive such a hidden reference, and therefore, they cannot refer to any instance members of anyobject instantiated from the class. They can only access class members of the same class.

Calling other constructors of the same class

Now I am going to discuss and illustrate the second common situation listed earlier.

A class can define two or more overloaded constructors having the same name and different argument lists. Sometimes it is useful for one overloadedconstructor to call another overloaded constructor in the same class. When this is done, the constructor being called is referred to as though it were a methodwhose name is this , and whose argument list matches the argument list of the constructor being called.

The sample program named This02

This situation is illustrated in the program named This02 shown in Listing 2 .

Listing 2 . The program named This02.
/*File This02.java Copyright 2002, R.G.BaldwinIllustrates use of this keyword for one overloaded constructor to accessanother overloaded constructor of the same class.Tested using JDK 1.4.0 under Win2000 The output from this program is:Instance variable myVar = 15 **************************************/class This02 { int myVar = 0;public static void main( String[]args){ This02 obj = new This02();obj.myMethod(); }//end main method//---------------------------------// //Constructor with no parameterspublic This02(){ //Call parameterized constructorthis(15); }//end constructor//---------------------------------// //Constructor with one parameterpublic This02(int var){ myVar = var;}//end constructor //---------------------------------////Method to display member variable // named myVarvoid myMethod(){ System.out.println("Instance variable myVar = " + myVar);}//end myMethod }//End This02 class definition.

Calling a noarg constructor

The main method in Listing 2 instantiates a new object by applying the new operator to the noarg constructor for the class named This02 .

(The common jargon for a constructor that doesn't take any parameters is a noarg constructor.)

The noarg constructor calls a parameterized constructor

The code in the noarg constructor uses the this keyword to call the other overloaded constructor, passing an int value of 15 as a parameter.

That constructor stores the value of the incoming parameter (15) in the instance variable named myVar . Then control returns to the noarg constructor, which in turn returns control to the main method. When control returns to the main method, the new object has been constructed, and the instance variable named myVar belonging to that object contains the value 15.

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