Re: java heap space
luca schwarz wrote:
Andrew Thompson schrieb:
luca schwarz wrote:
Please refrain from top-posting. I find it most confusing.
...
this is strange. when i run my main i get this error:
java.lang.OutOfMemoryError: Java heap space,
..
Your complete code tells the story. But first, I
will repost your code as an SSCCE*. An SSCCE
is a form of the code inteded to be a (short) self-contained,
compilable example of the problem.
Your code fulfilled most of that description, but
an extra tip is to rework it so it is all in 'one Java file'**
which can be achieved by demoting classes with no
'main()' from 'public' to '' (default/package).
** This is not good for 'real' code, but we are just
trying to sort a basic problem - so it is fine.
Here is what I mean..
<sscce>
public class Start {
public static void main(String[] args)
{
new Control();
}
}
class Control
{
// GOTO create model
Model theModel= new Model();
public Control()
{
System.out.print("Control-Constructor run");
}
}
class Model
{
// GOTO create control
Control theControl= new Control();
}
</sscce>
I put some comments in that code, to indicate why it
is failing, but here is a version that (to the best of my
current understanding of the code - which is 'not much')
does what you are attempting to achieve (without the
recursive element!).
<sscce>
public class StartOnce {
public static void main(String[] args)
{
new Control();
}
}
class Control
{
// Declare model
Model theModel;
public Control()
{
// hand the model a reference to this Control
theModel = new Model(this);
System.out.println("Control-Constructor run");
}
}
class Model
{
// Declare control
Control theControl;
Model(Control control) {
// associate the Control with our Model
theControl = control;
}
}
</sscce>
Each of control and model have a reference to 'the'
*single* instance of the other.
* The full description of the SSCCE, along with
why it is so handy for both debuggin and code
postings, can be found here.
<http://www.physci.org/codes/sscce/>
BTW - these are very simple questions, and there is
a group better suited to those learning Java, it is..
comp.lang.java.help
...I highly recommend it.
Andrew T.