Making a variable static

Discussion in 'Plugin Development' started by HackintoshMan, Apr 16, 2013.

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

    HackintoshMan

    I have heard that you are not supposed to make variable static, but eclipse wants to make a variable static. Have I heard this correct or does it really matter?

    [​IMG]
     
  2. Offline

    ZeusAllMighty11

    If possible, you should null all statics when the plugin is disabled, just because Java's Garbage Collector can miss it.

    I have some classes that use static methods, which obviously require static variables. Works fine for me, I don't have issues.


    Just make it static, but can I see what exactly it is?
     
  3. HackintoshMan
    Eclipse wants to make it static because most likely you are calling it like a static...
    Are you doing ClassName.method() or ClassName.field ? That is how you access static stuff from a class, since they can't be instanced.

    If you can't really pass the pointer to your class through then you can just make something static... or if lazyness occurs :}

    The Bukkit class contains static methods, before that class was created you had to pass the pointer to your main class to be able to access getServer() method.

    Static isn't a big issue with plugins since you can't have more than one instance of your plugin active, but make sure you fully understand them before using, I recommend googling for documentation.
     
    microgeek likes this.
  4. Offline

    Sagacious_Zed Bukkit Docs

    I would not go so far as to say you can't have more than one instance. You shouldn't have more than once instance, but there is nothing stopping it from creating more than one.
     
  5. Sagacious_Zed
    True about the main class but I meant how the Bukkit platform loads your plugin.
     
  6. Offline

    HackintoshMan

    Digi
    TheGreenGamerHD

    1) I access variable by MCFTF2.variable
    2) Eclipse made these static…

    Code:java
    1.  
    2. public static ScoreboardManager manager = Bukkit.getScoreboardManager();
    3. public static Scoreboard board = manager.getNewScoreboard();
    4. public static Team red = board.registerNewTeam("Red");
    5. public static Team blue = board.registerNewTeam("Blue");
    6. [syntax]
    7.  
    8. Also, Should I make all of the static variables null onDisable(){
    9. }?
    10.  
    11. EDIT: I think I need to take a break of java…I was thinking "on disable?" but i typed onDisable(){
    12. }
    13.  
    14. EDIT by Moderator: merged posts, please use the edit button instead of double posting.[/syntax]
     
    Last edited by a moderator: Jun 1, 2016
  7. Yes, because you're accessing them statically...

    Code:
    // static fields/methods
    Object.method();
    
    // and instanced
    Object obj = new Object();
    
    obj.method();
    You should read about instanced and static before using static because you're going to have issues from using them wrong on the long run, bugs which make no sense and stuff like that.

    I suggest you just carry your main plugin's instance through your other classes and use them instanced-style.

    Example of passing plugin into constructors of other classes: http://wiki.bukkit.org/Plugin_Tutorial#Using_a_separate_CommandExecutor_class
     
Thread Status:
Not open for further replies.

Share This Page