02100+02199 Indledende Programmering: Note on Parameter Passing

02100+02199  Indledende Programmering        Januar 2004
Note on Parameter Passing


By Paul Fischer

Example: Accessing methods of another class.

Three classes are defined, AClass, BClass, and CClass. AClass has (non-static) method methodA1. BClass wants to use that method. Class is the "master" class which uses both AClass and BClass. The structure is listed side by side below.


class AClass{            |     class BClass{              |  class CClass{
                         |                                |   
 public AClass(){        |      public BClass(){          |   public CClass(){
 }                       |      }                         |   }              
                         |                                |                                                         
 public int methodA1(){  |       public void methodB1(){  |    public static void main(String[] a){ 
                         |            ...                 |    AClass a = new  AClass();
   // commands           |            int n = methodA1(); |    BClass b = new  BClass();
                         |            //methodA1 unknown  |   
 }                       |       }                        |   }
                         |                                |
                         |                                |  
}                        |     }                          |  }
                         |                                |       



This will result in a compile time error at the line

                int n = methodA1();

because inside BClass the methods of AClass are unknown. Inorder to access the non-static methods of AClass, BClass has to have an instance of AClass. In the listing below we changed the constructor of BClass to have an instance od AClass as an argument. Then that can be used to call the methods of AClass. In CClass an instance of AClass is created and then givven to a BClass.


class AClass{             |  class BClass{                        |  class CClass{                               
                          |                                       |                                             
                          |    private AClass aClass;             |                                                     
                          |                                       |                                
 public AClass(){         |    public BClass(AClass ac){          |    public CClass{                                       
 }                        |      aClass = ac;                     |
                          |      // now an AClass is known        |    }                                     
                          |    }                                  |                                             
 public int methodA1(){   |                                       |    public static void main(String[] a){     
                          |    public void methodB1(){            |     AClass ac = new  AClass();               
   // commands            |      ...                              |     // ac is instance of AClass         
                          |      int n = aClass.methodA1();       |                                             
 }                        |      //methodA1() known as a          |     AClass b = new  BClass(ac);  
                          |      //method of aClass               |     // ac is given to BClass in the 
                          |    }                                  |     // constructor.
}                         |                                       |    }                                        
                          |  }                                    |
                          |                                       |  }
                          |                                       |


In the ccs-programs, there examples of this. AClass is like StatusPanel which provides a method to set the mouse coordinates. BClass is like the listener that knows the coordinates and wants to set them in the status panel. The listener gets a reference to a status panel in the constructor, so it can access the method to set the coordinates.


Hans Henrik Løvengreen, Jan 7, 2004