Work with several classes. The program does not start

I was working on a fairly large program and thought it was time to separate my classes. 1.java file for the GUI code and 1.java file for the mechanics behind the functions displayed by the graphical interface. But here is my problem, I created an instance of each class inside each other, and then the program refuses to run, so I'm obviously 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 I do this was to try it: Many of my variables are in the RPG class, and I want to be able to call them from my RPG and manipulate them, and then send them back to the 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 use to test the Mechanics class:

 mechanics.helloWorld(); 

Here is 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 is my question. Am I doing it right? What is wrong with my code that my program does not want to work?

ADDED: I tried to name my other classes as private , and the program compiles and still refuses to run and gives me the same error

Line 15 from Mechanics :

 public RPG rpg = new RPG(); 

line 127 from the RPG :

 public Mechanics mechanics = new Mechanics(); 
+4
source share
4 answers

Line 15 from 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; 
+4
source

This is because you are creating a new Mechanics class inside the RPG class. Then create a new RPG class inside the Mechanics class.

The result is an endless instance creation cycle.

In your specific example, I personally believe that the best way to fix the problem is to pass the RPG instance directly to the hello world method.

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

And then in your class the RPG will look something like this:

 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 } } 
+5
source

You create an endless loop when initializing an RPG or Mechanics instance. Object-oriented programming means separation of problems and low cohesion. Change the class dependencies so that only one of them needs the other.

+2
source

"I created an instance of each class inside each other"

This is your problem, the net results are as follows:

Class A is created, it has class B inside it, it creates a new class B
Class B is built, it has class A inside it, it creates a new class A Class A is built, it has class B inside it, it creates a new class B
Class B is built, it has class A inside it, it creates a new class A Class A is built, it has class B inside it, it creates a new class B
Class B is built, it has class A inside it, it creates a new class A

etc. etc. forever, deeper and deeper, until your program works.

There is nothing (horribly) wrong if class A has a link to class B and class B with reference to class A (although this may not be ideal), but there is something terrible with class A : encapsulating class B and class B encapsulating class A In the case where both links have links to each other, a link to one or both will be passed to one or both constructors, thereby not using the new keyword (even avoid this if possible, though).

An example of this I often (guilty) use the following:

 public class OwnerClass { SubordinateClass subOrdinate; public OwnerClass(){ subOrdinate=new SubordinateClass(this); } } public class SubordinateClass { OwnerClass owner; public SubordinateClass(OwnerClass owner){ this.owner=owner; } } 

Disclaimer: I do not represent this as good practice, but assuming that class A should talk to class B and vice versa, then this is achieved by the fact that

+1
source

Source: https://habr.com/ru/post/1481917/


All Articles