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

2013/6/9

The prefix static

Solringolt Solringolt

2013/6/9

#
What does it really do? Why shouldn't we using it always? I had a scenario in which I used many variables with static but don't really understand how it works. Can someone explain me?
Gevater_Tod4711 Gevater_Tod4711

2013/6/9

#
A variable that is declared static has the same value for every instance of a class (and all instances of the subclasses if it's public). Because it has the same value you don't need a specific object to get the value but can just get the value using the classname. This sometimes is easyer because you don't need any references but if you use this for e.g. a health counter of an object that will not work because if one instance of the class get's hit all other instances get the same damage. So it's sometimes easyer to declare variables static but in many cases that just doesn't work.
Solringolt Solringolt

2013/6/9

#
Ha ok! I just have a case like that, I'm trying to call a function of another class but it doesn't work. I wanted to use this but that only works for one object and here I have like 50 of them... My code is like that in explosion:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void checkIfBlockIsDestructible()
    {
        Actor destructibleBlock =(Block) getOneObjectAtOffset(0,0,DestructibleBlock.class);
        Actor undestructibleBlock =(Block) getOneObjectAtOffset(0,0,UndestructibleBlock.class);
         
        if (destructibleBlock != null)
        {
            destructibleBlock.remove();
            //getWorld().removeObject(destructibleBlock);
        }
        if (undestructibleBlock != null)
        {
            getWorld().removeObject(this);
            return;
        }
    }
The remove method is in the DestructibleBlock class but I got the error cannot find symbol - method remove() why is this not working and how can I get it working?
Gevater_Tod4711 Gevater_Tod4711

2013/6/9

#
The problem in this case is that the type of your reference is Actor and not DestructibleBlock (line 3) In Actor there is no such method and so you can't execute it. If you change the 'Actor' in line 3 to 'DestructibleBlock' and also change the cast (Block) to (DestructibleBlock) it should work. Also it would be possible to cast the reference before executing the method remove() like this: ((DestructibleBlock) destructibleBlock).remove();
Solringolt Solringolt

2013/6/9

#
What is exactly a cast? I'm trying to understand it, with some success but it's not 100% clear for me. Can you explain it to me?
Gevater_Tod4711 Gevater_Tod4711

2013/6/9

#
A cast is what you used in line 3 of your code. (The part: (Block) ). Using a cast you tell the programm what is the instance of a reference. Because all your subclasses of Actor are Actors it sometimes is necessary to tell the program what is the exact class of a reference (like in your example to execute the method remove()). Only if the type of the reference is right you can execute this method and if it's not right you need to cast it to the right class to tell the program that the reference is an instance of this class and this method is to be found in this class and so you can execute the method.
Solringolt Solringolt

2013/6/9

#
Thanks for all these information! That helps me a lot!
danpost danpost

2013/6/9

#
Actually, 'cast' (as a noun) is short for 'the casted type'. 'Type' is what class that the object belongs (or is known to be of). 'Cast'ing is what you do to inform the compiler that an object belongs to a particular class. 'Type casting' often needs to be done to gain access to methods and fields of the class the object was created with. The methods provided in the World and Actor classes that return an object or lists of objects return them as Actor class or Object class objects. Let us say you used 'getObjects(Ghost.class)' to return all the objects in your world in a Pacman scenario when a big dot is eaten and all ghosts need to be set to vulnerable. The method 'getObjects' returns a list of objects whose type is cast to the Object class. To access the 'setVulnerable' method in the Ghost class, the compiler needs to know it has to look there for the method. To accomplish that, we can type-cast the objects returned to 'Ghost'. The code for this would look something like this:
1
2
3
4
5
6
7
8
9
10
11
12
// in Pacman class
Actor bigdot = getOneIntersectingObject(BigDot.class);
if (bigdot != null)
{
    getWorld().removeObject(bigdot);
    //  add to score for eating big dot
    //  play sound for eating dot
    for (Object obj : getWorld().getObjects(Ghost.class))
    {
        ((Ghost)obj).setVulnerable();
    }
}
Solringolt Solringolt

2013/6/9

#
Haa ok that is because getObjects return a list of objects we do a for)? what kind of operator is the " : "?
Gevater_Tod4711 Gevater_Tod4711

2013/6/9

#
The : in the for loop is a special kind of for loop called for-each loop. In this loop the object obj (int danposts example) first gets the value of the first element in the list then the second... So the loop is executed once for each object in the list (thats the cause for the name for-each loop). This kind of loop can be used with arrays or every class that implements the interface java.lang.Iterable.
danpost danpost

2013/6/9

#
The 'for' is used to iterate through the list of objects that is returned. Line 8 above could be read as 'for each object returned from the list of Ghost objects in the world, do the following'. The ':' symbol is a separator (not an operator). It seperates the list (on the right) from the field that each item in the list is to be stored in when it is that items turn in the loop.
Solringolt Solringolt

2013/6/9

#
Thx for all these instructive precisions!
You need to login to post a reply.