getConfig not work in public static

Discussion in 'Spigot Plugin Development' started by --TOMAS--, May 15, 2016.

  1. This is the code:
    Code (Text):
    public static String sinpermisos = getConfig().getString("Messages.noPermission");
    Error in: getConfig()
     
  2. show us the full error....
     
  3. Are you in your main class?
    What is the error
    Why do you need the String to be static?
     
  4. Yes, i am in main class.
    Error: Cannot make a static reference to the non-static method getConfig() from the type JavaPlugin
     
  5. Answer the third question?
     
  6. Error: Cannot make a static reference to the non-static method getConfig() from the type JavaPlugin
    I want use this variable in external class, in another package and class.
     
  7. Then use an instance
     
    • Agree Agree x 1
  8. Assign sinpermisos in the onEnable method
     
  9. I want use this variable in external class, in another package and class. If I do not put the static variable, this brand mistake and asks that makes static.
     
  10. I'm trying to help you, so I'm telling you, don't abuse static, use an instance.
     
  11. Well, i will try it.
     
  12. You can set the static variable from onEnable().

    Code (Text):

    public static String test;

    @Override
    public void onEnable () {
    test = getConfig()...
    }
     
    Edit: As @Redrield said, you shouldn't use static fields and methods with the Bukkit API.
     
  13. I wouldn't say that you can't use them, but in the way he's using them it's just abusing static.
     
  14. No.. no..
    static.. no..
    Instances.
    Constructors if you want that way.

    A main instance is like this (or something like it.. don't remember it exactly. Just use constructors).
    Code (Java):
    public static final Main getPlugin() { return (Main)Bukkit.getPlugin(Main.class); }
     
    • Agree Agree x 1
  15. Your method and @Redrield work! Thanks :D
     
  16. Why so much trouble with static abuse?
     
  17. That's the exact thing I told you to do but that's none of my business :p
     
  18. Sorry, it was also your method.
     
  19. In normal Java development, static can be useful. But because of the way the Bukkit API loads the plugins and handles them, static can be troublesome and can lead to lots of bugs.
     
  20. Because it's uneeded.
    It's like having an email written on your pan while in the grocery store.
    Everyone sees it, but just one will actually use it (aka a 9 year old, but same shit here)

    You can just pass them a note with it.

    Instances & constructors are this note.