Static modifier's

Discussion in 'Plugin Development' started by DuskFireHD, Jan 9, 2015.

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

    DuskFireHD

    Hello people, Heaps of people have been telling me not to use static modifier's in my code is this true? thanks :D
     
  2. Offline

    mythbusterma

    @DuskFireHD

    Don't use them when they are not necessary/appropriate.
     
  3. Offline

    teej107

    It depends. Static destroys Object Oriented Programming which you should structure your code by. If you are using it solely to access a field from another class, you are using it wrong! Here is a good rule to decide whether a field should be static: If it makes sense to and if you can make it final. If you can do those, then sure make it static.
     
  4. Offline

    mythbusterma

    @teej107

    Not everything that is static should be final. There are usages for 'static' other than constants.
     
  5. Offline

    teej107

    Not including methods though. It is a good rule to ackowledge when using static especially when someone has questions about it.
     
  6. Offline

    mythbusterma

    @teej107

    I understand, I'm talking about static fields. Like:

    Code:
    private static int instances;
    
    public Constructor() {
        instances++;
    }
    Is a relatively valid use of static.
     
    leon3001 and teej107 like this.
  7. Offline

    teej107

    Well in that case, I would go by your advice.
    Some people don't know when they are necessary and use them inappropriately though. But you have proved your point!
     
    mythbusterma likes this.
  8. Offline

    DuskFireHD

    @mythbusterma Thanks but here is my problem and this code here is without any static's but it still don't work? I have done it with static's and it works but im trying not to use them

    MainClass:

    package api;

    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    import api.scoreboard.lobbysb;
    import api.scoreboard.sbmanager;
    import api.methods.methods;

    public class main extends JavaPlugin {

    methods methods;
    lobbysb lobbysb;
    sbmanager sbmanager;

    @Override
    public void onEnable(){
    getLogger().info("Arcade now enabled!");
    registerListeners();
    }

    @Override
    public void onDisable(){

    }
    public void registerListeners(){
    PluginManager pm = getServer().getPluginManager();

    pm.registerEvents(new sbmanager(this, lobbysb),this);
    pm.registerEvents(new lobbysb(this, methods), this);
    }

    }

    Scoreboard register:

    package api.scoreboard;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;

    import api.main;

    public class sbmanager implements Listener {

    main plugin;
    lobbysb lobbysb;

    public sbmanager(main instance, lobbysb lobby){
    plugin = instance;
    lobbysb = lobby;
    }


    @EventHandler
    public void onJoin(PlayerJoinEvent e) {
    final Player p = e.getPlayer();
    lobbysb.makeScoreboard();
    p.setScoreboard(lobbysb.board);
    }
    }

    Scoreboard:

    package api.scoreboard;

    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
    import api.main;
    import api.methods.methods;

    public class lobbysb implements Listener {

    main plugin;
    methods methods;

    public lobbysb(main instance, methods meths){
    plugin = instance;
    methods = meths;
    }

    public Scoreboard board;


    @SuppressWarnings("deprecation")
    public void makeScoreboard() {
    board = Bukkit.getScoreboardManager().getNewScoreboard();

    final Objective o = board.registerNewObjective("Test", "Test2");

    o.setDisplaySlot(DisplaySlot.SIDEBAR);

    Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    @Override
    public void run(){

    o.setDisplayName("§f§lStarting in: " + methods.getLobbyCountDown());

    Score kit1 = o.getScore("§3Kit:");
    kit1.setScore(4);
    Score kit2 = o.getScore("§f- Coming soon!");
    kit2.setScore(3);

    Score players1 = o.getScore("§3Players:");
    players1.setScore(2);
    Score players2 = o.getScore("§f- " + Bukkit.getOnlinePlayers().length + "/" + methods.getMaxPlayers());
    players2.setScore(1);
    }
    }, 0, methods.timePerSecond());
    }
    }
     
  9. Offline

    Skionz

    @DuskFireHD Your code is still hard to read. Please use naming conventions.
     
  10. Offline

    teej107

    Are you serious? You say his code is hard to read because of naming conventions? I would've thought you said his code is hard to read because he isn't using the code tags! xD


    @DuskFireHD Use code tags. And naming conventions do make code easier to read.
     
  11. Offline

    Skionz

    Good point xD
    I originally thought this
    Code:
    lobbysb lobbysb;
    was a syntax error until I looked at his imports.
     
    ChipDev likes this.
  12. Offline

    1Rogue

    Skionz likes this.
  13. Offline

    Ozcr

    In some situations they can be useful, but it's good practice to try and use them as little as possible.
     
  14. Offline

    1Rogue

    They are a tool to be used. They have a time and a place, but many people do not understand what that is (it's also hard to explain for every situation).
     
Thread Status:
Not open for further replies.

Share This Page