java - Working with multiple Classes. Program won't launch -
i've been working on rather large program, , thought time split classes. 1 .java file gui code, , 1 .java file mechanics behind functions gui presents. here's issue, i've created instance of each class inside each other, , program refuses launch, i'm doing wrong. in rpg
class have following line of code:
public mechanics mechanics = new mechanics();
and mechanics
class, have code:
public rpg rpg = new rpg();
the reason why i'm doing try this: lot of variables in rpg
class, , want able call them rpg
, manipulate them, send them rpg
here code used test function (from 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
public int
in rpg
class.
and in rpg
class, code using test mechanics
class:
mechanics.helloworld();
here's problem: code compiles, when try run it, error:
@ mechanics.<init>(mechanics.java:15) @ rpg.<init>(rpg.java:127)
here's question. doing right? whats wrong code that's making program not want run?
added: have tried called other classes private
well, , program compile, , still refuses launch, , feeds me same error
line 15 of mechanics
:
public rpg rpg = new rpg();
line 127 of rpg
:
public mechanics mechanics = new mechanics();
line 15 of mechanics
might more like:
public rpg rpg = new rpg(this); // must in constructor or non static method
in rpg:
public mechanics mechanics;
in constructor:
this.mechanics = mechanics;
Comments
Post a Comment