Rogue
Rogue

Reputation: 11483

Passing a returned value to an anonymous class

I am attempting to pass the return value from scheduling a task into an anonymous class, but I am running into trouble. If I set the return value to a final variable, it says it is not initialized:

/* Not initialized */
final BukkitTask task = Bukkit.getScheduler().runTaskTimer(plugin, new Runnable() {

    public void run() {
        /* irrelevant code */
        task.cancel();
    }

}, 0L, 20L);

I have also attempted passing a variable by calling a method within the anonymous class, however it changes the return type to void and therefore I cannot pass a proper value:

BukkitTask temp = null;
/* Returns void */
temp = Bukkit.getScheduler().runTaskTimer(plugin, new Runnable() {

    private BukkitTask task;

    public void initTask(BukkitTask task) {
        this.task = task;
    }

    public void run() {
        /* irrelevant code */
        task.cancel();
    }

}.initTask(temp), 0L, 20L);

How can I pass a returned value into an anonymous class within the code?

Upvotes: 4

Views: 229

Answers (5)

Tobias Brandt
Tobias Brandt

Reputation: 3413

You can define this class

class Box<T> {
    public volatile T value;
}

and use it like this:

final Box<BukkitTask> taskBox = new Box<BukkitTask>();
taskBox.value = Bukkit.getScheduler().runTaskTimer(plugin, new Runnable() {
    public void run() {
        /* irrelevant code */
        taskBox.value.cancel();
    }
}, 0L, 20L);

However, taskBox.value in run could still be null depending on when runTaskTimer actually executes the runnable.

Upvotes: 3

jrey
jrey

Reputation: 2183

see this example

 public static void main(String[] args) {
    final Object objectA[] = new String[1];

    new Thread(new Runnable() {
        private A refA = null;
        public void run() {
            objectA[0] = "Hello World or your object";
        }
    }).start();;

    while (objectA[0] == null){

    }
    System.out.println(objectA[0]);
}

Upvotes: 0

An SO User
An SO User

Reputation: 24998

My idea, which might sound stupid, would be to create a global variable to which you assign the return value. Then, have a static method that would return this value to you in the anonymous class.

Upvotes: 1

Simon Stastny
Simon Stastny

Reputation: 51

I am not sure what are you trying to do. Would Callable instead of Runnable solve the problem? http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Callable.html

Upvotes: 0

Rogue
Rogue

Reputation: 11483

I seem to have figured out an answer rather quickly!

You can change the return type of the init method, and then it will return the proper values for the code!:

BukkitTask task = null;
task = Bukkit.getScheduler().runTaskTimer(plugin, new Runnable() {

    private BukkitTask task;

    public Runnable initTask(BukkitTask task) {
        this.task = task;
        return this;
    }

    public void run() {
        /* irrelevant code */
        task.cancel();
    }
}.initTask(task), 0L, 20L);

Upvotes: 0

Related Questions