2

I am trying to make a plugin that uses a scheduleSyncRepeatingTask and the task is created by calling a method. The problem is trying to call it from a different class.

When I try to call the method, it will say that the method needs to be static. So, I make the method static and then the first parameter in the scheduleSyncRepeatingTask says it cannot be used in a static context.

My Method

public static void newCountdown() {
    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
        public void run() {
            for (Player player : Bukkit.getServer().getOnlinePlayers()) {
                player.sendMessage("Hey");
            }
        }                   
    }, 0, 20);
}

I'm kind of new to Java at the minute and static variables are still something I'm trying to understand so if anyone can link me to somewhere I can read up on it, or even a video then that would be great.

As for the problem, if anyone has the solution then any help would be greatly appreciated.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
Jordan
  • 323
  • 1
  • 14
  • 1
    'this' is a reference to an object to which you do not have access from static (ie. class) method. Class methods know nothing about particular instances. – Artur Jul 06 '15 at 15:59

3 Answers3

3

Check out this post to get a quick and easy explanation of the static keyword.

To avoid the error saying the method needs to be static call the method on an instance rather than the class.

Like this:

Foo f = new Foo();
f.newCountdown();

instead of:

Foo.newCountdown();
Community
  • 1
  • 1
CWhitten
  • 91
  • 3
  • Hey, thanks for your answer. I have used this in another plugin that I have made and It works fine. The Plugin I'm doing now though doesn't seem to let it work and I think it might have something to do with the class being in a different package. Could this be the case? – Jordan Jul 06 '15 at 19:00
  • The package wouldn't matter. Did you try removing `static` from your method signature and calling the method on an instance of your class? – CWhitten Jul 06 '15 at 19:42
  • Yes, the method is not static but when I use the bit to call an instance instead of the class, that is the part that gives me the error in console.. No matter where I put it, as soon as it executes that line of code it dies. – Jordan Jul 06 '15 at 19:54
  • The error is too long for a comment so I have uploaded it to http://pastebin.com/E8iDZPAT – Jordan Jul 06 '15 at 20:15
  • I don't have any experience with bukkit but I found [this thread](https://bukkit.org/threads/plugin-already-initialized.322695/) which I hope helps. – CWhitten Jul 06 '15 at 20:22
  • Hey, so that thread you linked sort of helped. Basically the problem now is if I do `static package.Main main = new package.Main();`, this does not work unless I don't use static. Any Ideas? – Jordan Jul 06 '15 at 21:48
  • Hey, could you take a look at my new question? Thanks http://stackoverflow.com/questions/31256521/calling-the-method-on-an-instance-rather-than-the-class-not-working-with-static – Jordan Jul 06 '15 at 23:48
0

Static methods refers to the class, not to a specific instance. They are common to all instances of the class.

You can call a static method without any instance and its why you can refer to this inside it.

Rodolfo
  • 1,091
  • 3
  • 13
  • 35
0

You cant get access to this, on a static method. Please have a look here: Oracle Understanding Class Members

melli-182
  • 1,216
  • 3
  • 16
  • 29