Eight Queens Recursive I am working on a Java textbook chapter on recursion, and I am to implement an Eight Queens solution using recursion.  Though I know that I am to post working code in Code Review, I do believe this code would work if I didn't run out of memory.  I have walked through the debugger, and the queens seem to adjust position exactly like they should until there is a Stack Overflow.

My question really is this:  After running into this Stack Overflow issue, my instincts are to either find a way to increase Stack memory (I have not found a way to do this) or reduce Stack memory usage.  To reduce Stack memory usage, I do wonder if there might be a way to put some of the weight of the recursion on the heap instead.  The most likely way to reduce Stack memory usage would likely be some clever refactoring, which is why I am posting in Code Review.  What I feel the best answer for this is, is just to ditch as much recursion as I can, but if I am to follow the exercise instructions, I don't have that option.

This is what I have:

Queen.java

class Queen {
    private int row;
    private int col;
    private int qNum;

    Queen(){
        row = 0;
        col = 0;
    }

    int getRow() {
        return row;
    }

    int getCol() {
        return col;
    }

    void setQNum(int qNum){
        this.qNum = qNum;
    }

    // requires canAdvance check first
    void advance(){
        if (col == 7){
            ++row;
            col = 0;
        }
        else{
            ++col;
        }
    }
    void matchPosition(Queen previousQueen){
        if (previousQueen.getCol() == 7){
            row = previousQueen.getRow() + 1;
            col = 0;
        }
        else{
            row = previousQueen.getRow();
            col = previousQueen.getCol() + 1;
        }
    }

    boolean canAdvance(){
        return !(row == 7 &amp;&amp; col == 7);
    }

    private boolean isAttacked(Queen attacker){
        return getRow() == attacker.getRow() ||
                getCol() == attacker.getCol() ||
                Math.abs(getRow() - attacker.getRow()) ==
                        Math.abs(getCol() - attacker.getCol());
    }

    boolean isSafe(Queen[] queens){
        for(int attIndex = 0; attIndex &lt; qNum; ++attIndex){
            if(this.isAttacked(queens[attIndex])){
                return false;
            }
        }
        return true;
    }
}


Main.java

class Main{
    public static void main(String[] args){
        Queen[] queenArr = {
                new Queen(),
                new Queen(),
                new Queen(),
                new Queen(),
                new Queen(),
                new Queen(),
                new Queen(),
                new Queen()
        };

        // Storing array index numbers allow the code to be more readable.
        for(int i = 0; i &lt; 8; ++i){
            queenArr[i].setQNum(i);
        }

        placeQueen(queenArr, 1);
        for(Queen queen : queenArr){
            System.out.printf("row = %d  col = %d%n", queen.getRow(), queen.getCol());
        }
    }

    private static void placeQueen(Queen[] queens, int index){
        if (queens[index].canAdvance()){
            queens[index].advance();
        }
        else{
            while(index &gt; 0 &amp;&amp; !queens[index].canAdvance()){
                --index;
            }
            placeQueen(queens, index);
        }

        if (queens[index].isSafe(queens)){
            if (index == 7){
                return;
            }
            else{
                // next queen is set to position of current queen
                queens[index + 1].matchPosition(queens[index]);
                ++index;
            }
        }
        placeQueen(queens, index);
    }
}
I think the place where recursion is least appropriate is when you advance a queen until it is safe from all queens with a lower index (i.e. the last line of the method placeQueen(Queen[], int)). This doesn't really seem like a recursive process to me, but rather like something that calls for a loop.

private static void placeQueen(Queen[] queens, int index) {
    while (!queens[index].isSafe(queens) &amp;&amp; queens[index].canAdvance()) {
        queens[index].advance();
    }

    if (!queens[index].canAdvance()
            &amp;&amp; (!queens[index].isSafe(queens)
            || index != 7)) {
        while (index &gt; 0 &amp;&amp; !queens[index].canAdvance()) {
            --index;
        }
        queens[index].advance();
        placeQueen(queens, index);
    } else if (index != 7) {

        // next queen is set to position of current queen
        queens[index + 1].matchPosition(queens[index]);
        ++index;
        placeQueen(queens, index);
    }
}


This advances a queen until it is either safe or can no longer be advanced, which is basically what your code did, but here, this is achieved without recursion, saving a lot of stack memory.