<< Chapter < Page Chapter >> Page >

Display the value of the instance variable

The next statement in the main method calls the method named myMethod on the object, which causes the value stored in the instance variable (15) to be displayed on the screen.

The most important statement

For purposes of this discussion, the most important statement in the program is the statement that reads:

this(15);

This is the statement used by one overloaded constructor to call another overloaded constructor.

Callbacks

An extremely important concept in programming is the third situation mentioned in the earlier list . This is a situation where a method in one object calls a method in another object and passes a reference to itself as aparameter.

(This is sometimes referred to as registration. That is to say, one object registers itself on another object.)

The method in the second object saves the reference that it receives as an incoming parameter. This makes it possible for a method in the second object tomake a callback to the first object sometime later. This is illustrated in the program named This03 , shown in Listing 3 .

Listing 3 . The program named This03 .
/*File This03.java Copyright 2002, R.G.BaldwinIllustrates using the this keyword in a callback scenario.Tested using JDK 1.4.0 under Win2000 The output from this program is:Instance variable myVar = 15 **************************************/class This03 { public static void main(String[] args){ClassA objA = new ClassA(); ClassB objB = new ClassB();objA.goRegister(objB); objB.callHimBack();objA.showData(); }//end main method}//End This03 class definition. //===================================//class ClassA{ int myVar;void goRegister(ClassB refToObj){ refToObj.registerMe(this);}//end goRegister //---------------------------------//void callMeBack(int var){ myVar = var;}//end callMeBack//---------------------------------// void showData(){System.out.println( "Instance variable myVar = "+ myVar); }//end showData}//end ClassA //===================================//class ClassB{ ClassA ref;void registerMe(ClassA var){ ref = var;}//end registerMe //---------------------------------//void callHimBack(){ ref.callMeBack(15);}//end callHimBack }//End ClassB class definition

Not intended to be useful

Note that the program in Listing 3 is intended solely to illustrate the concept of a callback, and is not intended to doanything useful. This is a rather long and convoluted explanation, so please bear with me.

The main method begins by instantiating two objects, one each from the classes named ClassA and ClassB .

Go register yourself

Then the main method sends a message to objA telling it to go register itself on objB . A reference to objB is passed as a parameter to the method named goRegister belonging to objA .

The code in objA uses this reference to call the method named registerMe on objB , passing this as a parameter. In other words, the code in objA calls a method belonging to objB passing a reference to itself as a parameter. The code in objB saves that reference in an instance variable for later use.

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