// Recursion example that involves an object creating new objects

import javax.swing.JOptionPane;

public class Recursion3
private int num;
Recursion3 next;
// constructor:

public Recursion3(int a_num)
{     num = a_num;
    if (num > 1)
         next = new Recursion3(num-1);
    factorial();
}

public int factorial()
{     if (num == 1) return 1;
    else
    return num * next.factorial();
}

public static void main(String[] args)
{     int num = 17;
    Recursion3 r = new Recursion3(num);
    int result = r.factorial();
    String msg = num + "! = " + result;
    JOptionPane.showMessageDialog(null, msg);
}
}