Solved Is this bad practice?

Discussion in 'Plugin Development' started by TehHypnoz, Jan 27, 2015.

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

    TehHypnoz

    Let's say I have 2 classes:

    Main.java
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
            if (label.equalsIgnoreCase("kickme") ) {
                KickPlayer.kickPlayer(p);
            }
            return true;
        }
    
    KickPlayer.java
    Code:
    public static void kickPlayer(Player p) {
    // code here
    }
    
    I have read multiple posts of people saying you should try to not use static when it's not needed, so is this bad practice? If so, why? Is there a better way of doing this?
     
  2. Offline

    mythbusterma

    @TehHypnoz

    Well first off, Player#kickPlayer(..) works just fine.

    Second, if that class is a utility class (i.e. has ZERO, ZIP, NADA, ZILCH fields that are required for it), then having it be static is fine. However, if it has instance variables that are required for it, for example things like a reference to your main plugin, then you should keep track of an instance of it.

    This is best practice because exposing static fields can lead to memory leaks, unintentional reassignment, unexpected behavior, difficult to read code, and violates core OOP principles.
     
    TehHypnoz and FerusGrim like this.
  3. Offline

    TehHypnoz

    I know that I can just use that, it was just an example that I came up with. But if I would make a class with just methods, no strings, booleans etc. could I just use static?

    Also, you probably already noticed but I don't have a lot of Java knowledge yet, I'm probably going to start watching some Java tutorials because this is not the first time I have ran into a problem that is not really Bukkit related.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  4. Offline

    FerusGrim

    As a rule of thumb, static is - or can be - appropriate in any place where the method only requires the variables that are being passed to it.

    EDIT: Also, if you're going to further your Java knowledge, may I recommend The Java Tutorials? Watching videos is a good way to learn bad practice.
     
    TehHypnoz likes this.
  5. Offline

    TehHypnoz

    What do you mean with 'Watching videos is a good way to learn bad practice.'? Would it be better to learn from written tutorials rather then one of those Java tutorial series on YouTube for example?
     
  6. Offline

    FerusGrim

    It would be so much better, it's almost surreal to think of that as a real question.
     
  7. Offline

    mythbusterma

    That is the idea of a utility class.
     
    FerusGrim likes this.
  8. Offline

    TehHypnoz

    FerusGrim likes this.
  9. Offline

    _Filip

    FerusGrim likes this.
Thread Status:
Not open for further replies.

Share This Page