Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with multiple Classes. Program won't launch

I've been working on a rather large program, and thought it was time to split up my classes. 1 .java file for the GUI code, and 1 .java file for the mechanics behind the functions the GUI presents. But here's my issue, I've created an instance of each class inside each other, and the program then refuses to launch, so I'm clearly doing something wrong. In my RPG class I have the following line of code:

public Mechanics mechanics = new Mechanics();

And for my Mechanics class, I have this code:

public RPG rpg = new RPG();

The reason why I'm doing this WAS to try this: A lot of my variables are in the RPG class, and I want to be able to call them from my rpg and manipulate them, then send them back to RPG Here is the code I used to test this function (from my Mechanics class):

class Mechanics{
public RPG rpg = new RPG();
  public Mechanics(){
  }
  public void helloWorld(){
    System.out.println("Hello World!");
    System.out.println("Health before:"+rpg.Health);
    rpg.Health = rpg.Health - 5;
    System.out.println("Health after:"+rpg.Health);
  }
 }

Yes, Health is a public int in my RPG class.

And in my RPG class, this is the code I am using to test my Mechanics class:

mechanics.helloWorld();

Here's my problem: The code compiles, but then when I try to run it, I get this error:

 at Mechanics.<init>(Mechanics.java:15)
 at RPG.<init>(RPG.java:127)

enter image description here Here's my question. Am I even doing this right? Whats wrong with my code that's making my program not want to run?

ADDED: I have tried called my other classes as a private as well, and the program will compile, and still refuses to launch, and feeds me the same error

Line 15 of Mechanics:

public RPG rpg = new RPG();

line 127 of RPG:

public Mechanics mechanics = new Mechanics();
like image 703
user2388169 Avatar asked Dec 16 '22 10:12

user2388169


2 Answers

It's because you instantiating a new Mechanics class inside of the RPG class. Then instantiating a new RPG class inside the Mechanics class.

The result is an infinite loop of instantiation.

For your specific example, I personally think the best way to fix the issue would be to pass the RPG instance directly into the hello world method.

class Mechanics {
    public void helloWorld(RPG rpg) {
        ...
    }
}

And then in your RPG class would look something like:

class RPG {
    // passing a mechanics object in via the constructor would be better than hard-coding it here
    public Mechanics mechanics = new Mechanics();

    public int Health = 100;
    ...

    public void someMethod() {
        mechanics.helloWorld(this); // pass in the rpg instance
    }
}
like image 106
Supericy Avatar answered Dec 27 '22 19:12

Supericy


Line 15 of Mechanics might be more like:

public RPG rpg = new RPG(this); // must be in constructor or non static method

In RPG:

public Mechanics mechanics;

in the constructor:

this.mechanics = mechanics;
like image 32
Andrew Thompson Avatar answered Dec 27 '22 18:12

Andrew Thompson