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

2019/2/25

How can I create an array which is public static?

Kili Kili

2019/2/25

#
I need help!
petyritonel petyritonel

2019/2/25

#
1
public static int[] arrayname = new int[3];
3 is the length of the array, you can change that. also it doesn't have to be int, i believe you can use other types too. this is just one array that i'm using currently, and it's working alright. if there's a better way, please correct me.
danpost danpost

2019/2/25

#
Kili wrote...
How can I create an array which is public static? I need help!
What is your array to be used for? (I question whether or not a public static array is appropriate for what you want to use it for)
Kili Kili

2019/2/26

#
I declare the array in the act method of the world and the actors should see the array. If I write
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class MyWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
 
    /**
     * Constructor for objects of class MyWorld.
     *
     */
    public static boolean[] alive = new boolean[3];
     
    public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1400, 800, 1);
         
    }
    public void act(){
         
    }
}
there is no Error but if I write
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class MyWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
 
    /**
     * Constructor for objects of class MyWorld.
     *
     */
     
     
    public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1400, 800, 1);
         
    }
    public void act(){
        public static boolean[] alive = new boolean[3];
    }
}
there are Errors Is there a possibility to declare the array in the act-method without any errors?
danpost danpost

2019/2/27

#
Kili wrote...
I declare the array in the act method of the world and the actors should see the array. If I write << Code Omitted >> there are Errors << Images Omitted >> Is there a possibility to declare the array in the act-method without any errors?
If you declared it in the act method, it will not have scope enough for any actor to "see" it (as soon as the act method completes, the array will no longer be in existence). Besides, you cannot declare static content within the act method. However, you can declare it outside the act method and then assign a value to it in your act method, like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import greenfoot.*;
 
public class MyWorld extends World
{
    public static boolean[] alive;
     
    public MyWorld()
    {
        super(1400, 800, 1);
    }
     
    public void act()
    {
        alive = new boolean[3];
    }
}
Kili Kili

2019/2/27

#
Thank you!
You need to login to post a reply.