This site requires JavaScript, please enable it in your browser!
Greenfoot back
rakesh8015
rakesh8015 wrote ...

2014/9/9

java.lang.OutOfMemoryError: Java heap space

rakesh8015 rakesh8015

2014/9/9

#
import greenfoot.*; public class jaffa extends World { private class1 cl1=new class1(); public jaffa() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); addObject(cl1,50,100); class2 cl2=new class2(); addObject(cl2,200,200); class3 cl3=new class3(); addObject(cl3,100,40); System.out.println(cl1.x); } public class1 getjaffa() { return cl1; } } program for class1 is below: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class class1 extends Actor { public int x=0; jaffa jaffa1=new jaffa(); private class1 ob=jaffa1.getjaffa(); public class1(){ System.out.println(ob.x); } public void act() { } }
davmac davmac

2014/9/9

#
Please use code tags when posting code. You create a new 'class1' when a 'jaffa' is created, and create a new 'jaffa' when a 'class1' is created. So you get infinite recursion. You should probably not be creating a 'new jaffa' inside class1. In terms of style, Java class names should start with a capital - 'Jaffa' not 'jaffa', 'Class1' not 'class1' etc. (Since you haven't said what you are actually trying to do, I can't give good advice on how to solve the problem.)
rakesh8015 rakesh8015

2014/9/9

#
thank you very much for the reply...actually i want to access "x" relating to object "cl1" which is created in "jaffa"
rakesh8015 rakesh8015

2014/9/9

#
i will also change the names and execute once again
danpost danpost

2014/9/9

#
This is very confusing. You have a class called 'jaffa' that has a method called 'getJaffa' that returns a 'class1' object. The class itself, 'jaffa', would create a 'jaffa' object; so, what actually is your 'getJaffa' method returning? ... not a 'jaffa' object, but a 'class1' object. I am so confused.
danpost danpost

2014/9/9

#
rakesh8015 wrote...
public class class1 extends Actor
{
    public int x=0;
    jaffa jaffa1=new jaffa();
    private class1 ob=jaffa1.getjaffa();
    public class1(){
        
            System.out.println(ob.x);
        }
   
    public void act() 
    {
    }
    
}
I do not believe any of the fields are necessary in the 'class1' class. You should be able to use this:
public class class1 extends Actor
{
    protected void addedToWorld(World world)
    {
        System.out.println("class1: "+getX());
    }
}
As the 'class1' object stored in 'cl1' in the 'jaffa' class should be the 'class1' object being created and added into the world. I added text to the print line to distinguish it from the output from the 'jaffa' world constructor. Your terminal should print:
50
class1: 50
Super_Hippo Super_Hippo

2014/9/9

#
@danpost: amazing story, if you ask me :D The world creates an actor and the actor creates a new instance of the world which just created it...
rakesh8015 wrote...
actually i want to access "x" relating to object "cl1" which is created in "jaffa"
Since you want to access the 'x' inside the 'cl1' class, you can just use the x (which will be 0 since you don't change it), instead of accessing the object stored in the world.
danpost danpost

2014/9/9

#
Super_Hippo wrote...
@danpost: amazing story, if you ask me :D
What 'story' :? amazing :? Please explain.
Super_Hippo Super_Hippo

2014/9/9

#
I had to laugh when I read this post ;)
danpost wrote...
This is very confusing. You have a class called 'jaffa' that has a method called 'getJaffa' that returns a 'class1' object. The class itself, 'jaffa', would create a 'jaffa' object; so, what actually is your 'getJaffa' method returning? ... not a 'jaffa' object, but a 'class1' object. I am so confused.
danpost danpost

2014/9/9

#
Super_Hippo wrote...
I had to laugh when I read this post ;) < Quote Omitted >
Oh. BTW, I left the best parts of the story out (felt they would be inappropriate).
You need to login to post a reply.