Setting Final Boolean Value

Discussion in 'Plugin Development' started by JungleSociety, Apr 4, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    JungleSociety

    Hello, this is probably a stupid question as my knowledge of java is small at the moment, but I'm working on it. Well anyways my problem is setting the value of a Boolean with the modifier Final. It appears I have to set the Booleans modifier to a final because I am running it in a Bukkit scheduler. So basically I try to set the value of a final Boolean and it says: "The final local variable isingame cannot be assigned, since it is defined in an enclosing type." Where isingame is my Boolean name. Here is a portion of my code:
    Code:java
    1. final boolean isingame = false;
    2. if(ingame.isEmpty()){
    3. isingame = false;
    4. }
    5. BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
    6. scheduler.scheduleSyncDelayedTask(this, new Runnable() {
    7. @Override
    8. public void run() {
    9. isingame = true;
    10. for(Player redplayers : red){
    11. World world = (World) getConfig().get("redspawn" + ".world");
    12. int x = (int) getConfig().get("redspawn" + ".x");
    13. int y = (int) getConfig().get("redspawn" + ".y");
    14. int z = (int) getConfig().get("redspawn" + ".z");
    15. Location loc = new Location(world,x,y,z);
    16. redplayers.teleport(loc);
    17. redplayers.sendMessage(ChatColor.RED + "You are on the red team!");
    18. }

    So as you can see I have set the Boolean isingame to a final modifier because of this scheduler. So my question is, how can I set a final Boolean's value, or how can I run a normal Boolean without the final modifier in a Bukkit Scheduler?


     
  2. Offline

    Europia79

    JungleSociety

    I have a couple of ideas.

    1. One involves making a new class and using the constructor to pass in values. Possibly more refactoring than you want to do (if you can avoid it).

    2. Maybe you could make a method. And define one of the method parameters to be final. Then call the method and pass in your boolean flag, isingame.

    For my code, I went with option one... So I know it works. But I've never actually tried option two, so I'm not sure if it'll work.
     
  3. Offline

    RawCode

    dont use nested classes and you wont hit such issue.
     
  4. Offline

    PogoStick29

    The variable needs to be final because it's local and you're using an inner class. I'd recommend moving the boolean to the class level. Also, why instantiate a Location for every single player if it's going to be the exact same?
     
  5. Offline

    xTrollxDudex

    JungleSociety
    Put in final Boolean array cap 1.

    Set 0 index to boolean

    Get boolean back from 0 index
     
  6. Offline

    Garris0n

    But my laziness :(
     
  7. Offline

    JungleSociety

    PogoStick29
    Thanks that makes so much sense now that I think about it.
     
  8. Offline

    Garris0n

    Cheater :p
     
  9. Offline

    xTrollxDudex

    Well, how about using an object wrapper?
     
    Garris0n likes this.
  10. Offline

    RawCode

    you can use blackmagic to change that field, it will be outlined to both classes - outer and nested.
     
Thread Status:
Not open for further replies.

Share This Page