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

2020/10/29

static and final ???

ronald ronald

2020/10/29

#
I still don't understand final and static I may read the tutorial several times can you explain them to me and tell me how to use them with short examples? thank you
danpost danpost

2020/10/29

#
The "final" keyword is used to "lock", or "fix", a value into a variable or prevent overriding of a method. Value fields (variables) become constants and Object reference fields cannot be reassigned. The locking takes place at the time the field is initialized (first assigned a value or object). The "static" keyword makes a field or method (aka. member) "abstract". The member becomes a class member instead of an instance member. As far as fields, then, one, and only one, is created. With instance fields, each instance of a class (object created from the class) is assigned its own independent set of fields. Static content is processed during compilation. Please note that resetting of a scenario does not re-compile your code; so care must be taken to reset static fields in your initial world constructor.
ronald ronald

2020/10/29

#
if i understand correctly with final i cannot change variable value, it is locked as you say, but can i use this value in all code or part of code ??? this is a question I ask myself. I'm trying to understand static, I will have other questions to ask you
danpost danpost

2020/10/29

#
ronald wrote...
if i understand correctly with final i cannot change variable value, it is locked as you say, but can i use this value in all code or part of code ???
A public static member can be accessed from anywhere using: <<Class name>>.<< member name>> Example: using:
1
2
3
4
5
6
7
8
9
public class MyWorld extends greenfoot.World
{
    public static int WIDE = 11, HIGH = 7;
     
    public MyWorld()
    {
        super(WIDE, HIGH, 40);
        prepare();
    }
you can have an Actor class with:
1
2
3
4
5
6
7
8
9
public void act()
{
    // create new larger world when clicked
    if (Greenfoot.mouseClicked(this) && MyWorld.WIDE < 21)
    {
        MyWorld.WIDE += 2;
        Greenfoot.setWorld(new MyWorld());
    }
}
A public static final member can also be accessed similarly, but would fail if you tried to change a field (constant) value.
ronald ronald

2020/10/29

#
for example if I put public static final int WIDE = 11, HIGH = 7 it will fail if I change the constants by putting for example 15 in WIDE if I am not mistaken with 15 in WIDE, it can work but would fail with 25 in WIDE or else it will both fail I don't know if I'm talking nonsense, hard to assimilate
danpost danpost

2020/10/29

#
ronald wrote...
for example if I put public static final int WIDE = 11, HIGH = 7 it will fail if I change the constants by putting for example 15 in WIDE
I was referring to like line 6 in my Actor class failing. You can obviously assign any initial value at line 3 in MyWorld.
ronald ronald

2020/10/29

#
i thought i understood your code but i don't if I change the constant of line 6, it will fail and line 4 of the actor class if I change the constant it will not fail, I think if I understand line 6 of the actor class, we add 2 to the constant I ask myself the question, I can modify the constant of line 4 but not line 6 of the actor class
danpost danpost

2020/10/29

#
Line 4 is a comparison and will not attempt to change the value of the static field. Line 6 changes the value of the static field, provided it is not a constant (modified with 'final').
ronald ronald

2020/10/29

#
even if I am yet to understand, I think that with time and practice, it will eventually assimilate
You need to login to post a reply.