getConfig() as a Static

Discussion in 'Plugin Development' started by 1Achmed1, Sep 22, 2013.

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

    1Achmed1

    So, I'm making a new plugin, but I call a string from my config twice. Now, as most of you know getConfig() can never be static, so how would one go about this? Here's the code BTW:
    Code:java
    1. String database = getConfig().getString("DatabaseName");
     
  2. Offline

    Trevor1134

    Can you explain more? Do you mean you are trying to access getConfig() in a different class rather than the main? Or...?
     
  3. Offline

    1Achmed1

    I am trying to access it more than once on the main
     
  4. Offline

    The_Doctor_123

    1Achmed1
    So the problem is..?

    Also, how can't getConfig() be accessed in a static manner?
    Code:java
    1. public static FileConfiguration config = getConfig();
     
  5. Offline

    1Achmed1

    Put this code into Eclipse:
    Code:java
    1. static String database = getConfig().getString("DatabaseName");
     
  6. Offline

    The_Doctor_123

    1Achmed1
    Oh, whoops, I forgot you can't do that.. Here's a work-around:

    Code:java
    1. public static String database;
    2.  
    3. @Override
    4. public void onEnable()
    5. {
    6. database = getConfig().getString("DatabaseName");
    7. }
     
  7. Offline

    1Achmed1

    Thankssss :)
     
  8. Offline

    S_Ryan

    Why would you ever need to access it statically?
     
    Mitsugaru likes this.
  9. Offline

    1Achmed1

    In my plugin I access it twice within the same class.
     
  10. Offline

    The_Doctor_123

    1Achmed1
    Do you know the definition of static?
     
  11. Offline

    1Achmed1

    Code:java
    1. static

    Is a keyword that can be used in 3 scenarios:
    • static variables
    • static methods
    • static blocks of code
    Static Variables (Our form of static)
    Static variables that belong to the class, not to an Object(instance)

    Static variables are initialized only once, at the start of the execution. These variables will be initialized first before the initialization of any instance variables

    A single copy to be shared by all instances of the class --> What we are using the keyword for

    A static variable can be accessed directly by the class name; It doesn't need any object


    If that's what you wanted?
     
  12. Offline

    PatoTheBest

    1Achmed1 Thank you! I always had curiosity, but I never had googled it.
     
  13. Offline

    The_Doctor_123

    1Achmed1
    Very good, but the one issue is, you don't need static variables for what you're doing..
     
    S_Ryan likes this.
  14. Offline

    PatoTheBest

  15. Offline

    The_Doctor_123

    PatoTheBest
    Very true, but he hasn't told us that. Here's what he said:

    Of course, maybe he isn't being specific enough.
     
  16. Offline

    1Achmed1


     
  17. Offline

    Coelho

    Pass the configuration (or the plugin itself) down the stack in the constructors.

    Singletons are bad. Never use them, especially in a non-singleton environment like the Bukkit API. This applies to static variables that are directly modified by a non-static call.
     
  18. Offline

    Janmm14

    Coelho
    I'm always creating a static variable instance and fill it in onEnable() and set it to null in onDisable(). I had no problems.
     
  19. Offline

    Coelho

    This is incorrect practice and I will continue to shun anyone who does this for their complete lack of knowledge regarding the Java programming language.
     
    S_Ryan likes this.
  20. Offline

    Janmm14

    Coelho
    be happy that I let varaibles start lowercase and classes uppercase :(((((((((((((((((((((((
     
    Coelho likes this.
  21. Offline

    S_Ryan

    Pass the same FileConfiguration object to every instance of the class....
    Creating a static FileConfiguration is a bad, improper use and should be avoided. The config is a single instance of an object and should not be used statically. Like I said before. Pass over the same variable.
     
Thread Status:
Not open for further replies.

Share This Page