SDSU CS 535: Object-Oriented Programming & Design
Fall Semester, 1997
Doc 11, Constructor Questions

To Lecture Notes Index
San Diego State University -- This page last updated 05-Oct-97

Contents of Doc 11, Constructor Questions

Calling Methods before Calling this in Constructor
class MethodCallInConstructor
   {
   private int value;
   
   public MethodCallInConstructor( int first, int second, int third )
      {
      this( nonstaticAdd( first, second, third ) );  // compiler error
      }

   public MethodCallInConstructor( int first, int second )
      {
      this( add( first, second ) );       // OK, can call static method
      System.out.println( "2 agruments: Value = " + value );
      }
   
   public MethodCallInConstructor( int first )
      {
      value = first;
      System.out.println( "1 agrument: Value = " + value );
      }
   
   public int nonstaticAdd( int a, int b, int c )
      {
      return a + b + c;
      }
      
   public static int add( int a, int b )
      {
      return a + b;
      }
   }

Doc 11, Constructor Questions Slide # 1
Nonstatic Methods in Constructors are Resolved Dynamically
class Parent
   {
   public Parent( )
      {
      whichOne( );
      }
   
   public void whichOne( )
      {
      System.out.println( "In Parent" );
      }
   }

class Child extends Parent
   {
   public void whichOne( )
      {
      System.out.println( "In Child" );
      }
   }

class Test
   {
   public  static  void  main( String  args[] ) 
      {
      new Child( );
      }
   }
Output
In Child



visitors since 05-Oct-97