1

Hello I'm still new to Java and OOP and am having issues trying to get my code to compile. I understand the issue with my code is the instantiation of the same object twice however I'm not sure how I can work around that to compile my code.

package week2;

import java.util.*

public class aBug {

    aBug theBug = new aBug();
    String Inspecies, Inname;
    int Inx, Iny, Inenergy, Inid;
    char Insymbol;

    Scanner scan = new Scanner(System.in);
    aWorld newWorld = new aWorld();

    public static void main(String[] args) {
        aBug theBug = new aBug();


        theBug.mainMenu();

    }

    public void mainMenu() {

        int choice;
        do {

            System.out.print("1\t Create bug\n");
            System.out.print("2\t Enter world\n");
            System.out.print("3\t Quit\n");
            choice = scan.nextInt();
            switch (choice) {

            case 1:
                bugInit();
                break;

            case 2:
                System.out.println();
                newWorld.mapInit(theBug.Inx, theBug.Iny, theBug.Insymbol);
                System.out.println();
                break;
            }
        } while (choice != 3);
    }

    public void bugInit() {
        String species, name;
        int x, y, energy, id;
        char symbol;



        System.out.print("Species: ");
        species = scan.nextLine();

        System.out.print("Name: ");
        name = scan.nextLine();

        System.out.print("X position: ");
        x = scan.nextInt();

        System.out.print("Y position: ");
        y = scan.nextInt();

        System.out.print("Energy: ");
        energy = scan.nextInt();

        System.out.print("ID: ");
        id = scan.nextInt();

        theBug.Insymbol = 'b';
        theBug.Inspecies = species;
        theBug.Inname = name;
        theBug.Inx = x;
        theBug.Iny = y;
        theBug.Inenergy = energy;
        theBug.Inid = id;

        System.out
                .format("\nThe bug is of the species %s, called %s, "
                        + "with positions %d & %d, with energy amount: %d, and %d as it's id number\n\n",
                        theBug.Inspecies, theBug.Inname, theBug.Inx, theBug.Iny,
                        theBug.Inenergy, theBug.Inid);

    }


}
2
  • Post the stack of errors you got. Commented Oct 19, 2014 at 18:10
  • Exception in thread "main" java.lang.StackOverflowError at week2.aBug.<init>(aBug.java:6) at week2.aBug.<init>(aBug.java:6) at week2.aBug.<init>(aBug.java:6) at week2.aBug.<init>(aBug.java:6) at week2.aBug.<init>(aBug.java:6) at week2.aBug.<init>(aBug.java:6) Commented Oct 19, 2014 at 18:14

1 Answer 1

1

In the constructor you have:

public class aBug {
    aBug theBug = new aBug();
...
}

So while creating an instance of aBug (for example in your main) you call new aBug(), which calls the constructor again recurrently with out end overflowing the stack.

I am not sure why do you think you need to create an instance of the object within itself. So it's hard to give any hints. If I guess correctly, you merged the idea of aBug and "main program" in one class. You should split it and put aBug internal stuff in its own class.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.