11.7 SELF-REFERENCE IN JAVA


11.7 SELF-REFERENCE IN JAVA

As mentioned already, Java also supports the keyword this for self-reference. As with C++, we can use this keyword to avoid name conflicts in the implementation code for a method or a constructor if convenience dictates that the identifier used for a data member have the same name as a parameter of the method or the constructor. The following example illustrates:

 
//SelfRef.java class X { private int n; //(A) public X( int n ) { this.n = n; } //(B) public static void main( String[] args ) { X xobj = new X( 20 ); System.out.println( xobj.n ); } }

Note that the name of the parameter in the constructor in line (B) is the same as the name of the data member in line (A). Yet, because we have accessed the data member by this.n, the compiler has no trouble keeping track of which n is which in the body of the constructor.

As was the case with C++, self-reference also plays an important role in defining methods that must return the object on which the method is invoked. Shown below is the Java version of the C++ program SpecialInt.cc of the previous section. The plus method in the Java version is defined as follows:

     SpecialInt plus( SpecialInt sm ) throws Exception {         accumulator += sm.getI();         if ( accumulator > 100 || accumulator < -100 )             throw new Exception();         return this;     } 

The return statement of this method uses the keyword this to return a reference to the object on which the method is invoked. So given repeated invocations as in line (C) below

     SpecialInt s1 = new SpecialInt( 4 );     SpecialInt s2 = new SpecialInt( 5 );     SpecialInt s3 = new SpecialInt( 6 );     SpecialInt s4 = new SpecialInt( 7 );     s1.plus( s2 ).plus( s3).plus( s4 );                          //(c) 

the first invocation of plus in s1.plus ( s2 ) returns a reference to the modified s1 object. The next invocation of plus is on this modified s1, and so on.

The complete program is shown below:

 
//SpecialInt.java class SpecialInt { int i; int accumulator; SpecialInt( int m) throws Exception { if (m > 100 || m < -100) throw new Exception(); i = m; accumulator = m; } int getI() { return i; } SpecialInt plus( SpecialInt sm ) throws Exception { accumulator += sm.getI(); if ( accumulator > 100 || accumulator < -100 ) throw new Exception(); return this; } public static void main( String[] args ) throws Exception { SpecialInt s1 = new SpecialInt( 4 ); SpecialInt s2 = new SpecialInt( 5 ); SpecialInt s3 = new SpecialInt( 6 ); SpecialInt s4 = new SpecialInt( 7 ); s1.plus( s2 ).plus( s3 ).plus( s4 ); System.out. println(s1.accumulator); // 22 //SpecialInt s5 = new SpecialInt(101); // range violation } }




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net