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

2014/5/10

Passing variables to other classes

gamer121 gamer121

2014/5/10

#
In my Ground class (subclass of World), I have a define variable which is 'private static final int CELL_SIZE = 20;' I want to use this variable in two of my subclasses of Actor. How can I do this?
danpost danpost

2014/5/10

#
Change it from 'private' to 'public' and use:
int cellsize = Ground.CELL_SIZE;
OR add the following method in the Ground class
public static int getCellSize()
{
    return CELL_SIZE;
}
and use this:
int cellsize = Ground.getCellSize();
CKnox CKnox

2014/11/25

#
@danpost, how are these methods used to define the variable size as 20?
danpost danpost

2014/11/25

#
The value of the constant CELL_SIZE in your Ground class is declared with an assigned value of 20. My last post shows ways you can use to have that value accessible to other classes.
You need to login to post a reply.