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

2019/11/14

Creating an instance Variable?

jstofer2 jstofer2

2019/11/14

#
Hello, I may just be having a brain fart. How do you create an instance variable in Greenfoot? Any clues would help. Thank you!
danpost danpost

2019/11/14

#
jstofer2 wrote...
How do you create an instance variable in Greenfoot?
Greenfoot uses Java; so you should ask how to create one there (in Java). In java, instance fields are defined as non-static variables within classes. There are not the local variables defined within methods -- they are declared outside of any method in the class. At minimum, you need a declared type and a legal (standard java naming procedure) user-defined name:
type name;
The type can any of the primitive types (int, double, long, boolean, byte, char, float ... others?) or it can be an Object type (Actor, World, String, Integer, etc. or the type of a class you have defined). An array is a type of Object, as well. The field declaration line can start with an access modifier (public, protected, private); it can be modified to make it a constant (final). The field can also be assigned an initial value. Examples follows:
private final int GRAVITY = 2;
protected int vSpeed;
public int[][] map = new int[10][10];
By convention, the names of constants are in all uppercase with words separated with an underscore ("_"). Normal variables are in lowercase characters with all but the first world beginning with an uppercase character. Only alpha characters and the underscore are allowed in names.
jstofer2 jstofer2

2019/11/18

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