java - Can someone explain me how this code works? (it's about using "final") -
this question has answer here:
i beginner in java programming , studying programming libgdx. in book "beginning java game development libgdx" there instructions on how make basic games. 1 of them (the game introduction processing mouse , touch input) make game balloons spawn on left side of screen , these balloons keep moving right side , each of them removed game either when click on it, or when goes off screen (when x greater width of screen) have problem understanding use of final
in code:
spawntimer += delta; // check time 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 if statement
responsible creating instance of actor called balloon
, add input listener
execute code:
popped++; b.remove(); return true;
when touched. in book final
wasn't used creating new instance of balloon
, used because android studio made me ( said couldn't use b.remove()
because code used inside instance or that) , seems works. want know why using final
works.
is final
object disposed of when method (update()) finished doing thing (and that's why won't bug line used again)?
when b.remove()
executed, how know going remove?
and when create instance thought giving name creating instance , pointer, right? so, if kind of loose pointer created can 1 inside of instance (or maybe mainstage
)?
so, when you're calling new inputlistener() {
... understand you're creating anonymous inner class. 1 of major benefits of using anonymous inner class can declare inside of class member (such method) , therefore inner class can have access elements of enclosing member, such variable balloon b
. have captured value that's variable of enclosing method. in other languages commonly called 'closure'. if examine bytecode produced, see new inner class have field of type balloon
, constructor parameter of type balloon
. using constructor, variable b
copied field of inner class.
before java, there have been plenty of programming languages allowed capture values in way, many of them have caused difficult-to-see problems in code, because variables can freely changed within method. touchdown
method of inner class not executed after b.addlistener
gets called. if code in enclosing method assigns different object b
before touchdown
executed? reference of b
should used when it's being executed? original one, or new one?
the way java solves problem insisting method variable captured inner class final, ensures not changed later in method. if attempt capture variable not final, compiler fail error.
as side-note, java 8 style lambda expressions compile down internally inner classes, have different rules capturing variables, results same. instead of insisting variable captured explicitly final, enclosing method statically analyzed ensure no code assigns variable. if does, error variable must 'effectively' final.
Comments
Post a Comment