I am a beginner in java programming and I am currently studying programming for libGDX. In the book "Beginning Java Game Development with libGDX" there are instructions on how to make some basic games. One of them (the game that is an introduction to processing mouse and touch input) we make a game where balloons spawn on the left side of the screen and these balloons keep moving to the right side and each of them is removed from the game either when you click on it, or when it goes off screen (when X is greater than the width of the screen)
I have a problem understanding the use of final
in this code:
spawnTimer += delta;
// check time for next balloon spawn
if(spawnTimer > spawnInterval)
{
spawnTimer -= spawnInterval;
final Balloon b = new Balloon();
b.addListener
(
new InputListener()
{
public boolean touchDown (InputEvent event, float x, float y, int pointer, int buttoon)
{
popped++;
b.remove();
return true;
}
}
);
mainStage.addActor(b);
}
The code inside of the if statement
is responsible for creating the instance of an actor called Balloon
and add to it an Input Listener
so it will execute this code:
popped++;
b.remove();
return true;
when it is touched. In the book final
wasn't used for creating a new instance of Balloon
and I only used it because Android Studio made me do it ( it said that I couldn't use b.remove()
because it is a code that will be used inside the instance or something like that) and it seems like it works. I want to know why using final
works.
Is the final
object disposed of when the method (update()) is finished doing its thing (and that's why it won't bug then the line is used again)?
When the b.remove()
is executed, how will it know what it is going to remove?
And when I create an instance like that I thought it was giving it a name but it is just creating an instance and a pointer, right?
So, if I kind of loose that pointer I just created I can only get another one from the inside of the instance (or maybe from the mainStage
)?