RECURSIVE AND CYCLIC CALLING

Recursive and Cyclic Calling


  • A Block named as Static inside a class is called Static Initialization Block(SIB).
  • SIB’s are invoked only once at the time of the corresponding loading class or reffered in to the memory.

Example 1:

 
						
                    package com.sdj.pack1;
                    public class Recursive
                    {
                       void test()
                       {
                          test();
                       }
                       public staticvoid main(String[] args)
                       {
                          new Recursive().test();
                       }
                    } 
                        

   Output:


    

Example 2:

 
						
                    package com.sdj.pack1;
                    public class Recursive
                    {
                       public Recursive()
                       {
                          this();
                          /*  Here compilation fails  because   constructor recursive  */
                          /*  calling won’t  reach object class  */
                       }
                       public static void main(String[] args)
                       {
                          new Recursive();
                       }
                    } 
                        

Cyclic Calling:

  • Cyclic is a continuous process that calls, first method to another method as well as calls another method to first method.

Syntax for Cyclic Calling :

 
						
                    class Cyclic
                    {
                       void m1()
                       {
                          m2();
                       }
                       void m2()
                       {
                          m1();
                       }
                    } 
                        
  • In case of method cyclic calling, compilation is success but running is fail then it is due to stack over flow error.

Example For Method Cyclic Calling:

 
						
                    package com.sdj.pack1;
                    public class Cyclic
                    {
                       void m1()
                       {
                          System.out.println("m1 type cyclic");
                          m2();
                       }
                       void m2()
                       {
                          System.out.println("m2 type cyclic");
                          m1();
                       }
                       /*  Here we are calling m2( ) from  m1( ) as well as m1( ) from m2( ). */
                       /*  This process will be continued and so exception will occur  */
                       /*  i.e,java.lang.StackOverflowError.  */
                       public static void main(String args[ ])
                       {
                          Cyclic c = new Cyclic( );
                          c.m1();
                       }
                    } 
                        

   Output:

    
    
    
    

Example For Constructor Cyclic Calling:

 
						
                    package com.sdj.pack1;
                    public class Cyclic
                    {
                       Cyclic()
                       {
                          this("SDJ");
                       }
                       Cyclic(String str)
                       {
                          this();
                       }
                       
                       /*  Here compilation fails because  constructor chaining  */
                       /*  has failed in  reaching the  object class.  */
                       public static void main(String args[ ])
                       {
                          Cyclic c = new Cyclic( );
                       }
                    }