-2

My main class starts with this:

public class ranks extends JavaPlugin implements Listener{

within that class, I have:

public static boolean isAdmin(String playerName){

    File adminFile = new File(this.getDataFolder() + File.separator + "admins.txt");

The problem is that I can't use "this". isAdmin MUST be static because in another class:

public class customInventory implements Listener{

I need to access it using:

if(!ranks.isAdmin(e.getPlayer().getName())){

As an overview, ranks uses methods from customInventory and vice-versa. Googling static methods and not being able to use "this" hasn't helped whatsoever.

Rixterz
  • 33
  • 5
  • 2
    it is java convention to capitalize the first letter of a class name – leeor Oct 17 '15 at 21:01
  • Maybe so. That doesn't solve my issue though – Rixterz Oct 17 '15 at 21:02
  • I find it pathetic how people downvote because I've come across a problem and need help solving it. That's what I wrote the god damn question for in the first place. – Rixterz Oct 17 '15 at 21:35

4 Answers4

1

static methods belong to the class, not a specific instance. this refers to an instance, and you don't have one. You need to make the isAdmin method an instance method (remove the static) and instantiate the rank class (with the new keyword) before calling the method.

Take a look at this answer for an explanation of static vs. instance state.

Community
  • 1
  • 1
leeor
  • 17,041
  • 6
  • 34
  • 60
  • now I'm getting an error on my server saying "Plugin already initialised!" in ranks I have: `customInventory cI = new customInventory(); @Override public void onEnable(){ this.getServer().getPluginManager().registerEvents(this, this);` and in customInventory I have: `ranks r = new ranks();` – Rixterz Oct 17 '15 at 21:12
  • It seems the code is a bit of a mess: why do you need absolutely static methods if you are actually creating an instance and could access the isAdmin method through that instance you mention? – gmcontessa Oct 17 '15 at 21:19
  • I can't access ranks.isAdmin from customInventory unless isAdmin is static. If isAdmin is static then I can't use "this". What am I supposed to do?! – Rixterz Oct 17 '15 at 21:20
  • if I create an instance of ranks anywhere then due to it extending JavaPlugin, it won't work because you can't have more than one class extending JavaPlugin – Rixterz Oct 17 '15 at 21:27
0

In Java, this refers to the object on which the current method is acting. But static methods don't act on objects, so there's nothing for this to refer to. If getDataFolter() is another static method, then you can call it as ranks.getDataFolder(). If it's an instance method, then you'll need to pass the relevant ranks instance into that method somehow.

user3553031
  • 5,990
  • 1
  • 20
  • 40
0

this means instance of class. But isAdmin is a static method. As you see when you try to access this, it's actually never created, there is no instance which you can access.

You can make getDataFolder static an then you can call it.

Design issue can be solved by basic DI;

public class Ranks extends JavaPlugin implements Listener{
     public boolean isAdmin(String playerName){
        //rest of business logic
     }
}


public class CustomInventory implements Listener{
    private Ranks rank;

    public CustomInventory(Ranks rank) {
       this.rank = rank;
    } 


   //then call this.rank.isAdmin as usual
}
Fatih Donmez
  • 4,319
  • 3
  • 33
  • 45
0

If the method getDataFolder is not inherited you can just make it static and call it without "this".

If it's inherited, so you cannot make the method static, then you need to create a static instance (singleton pattern) of rank class and use it to access the method.

gmcontessa
  • 558
  • 4
  • 10