Static Items

Discussion in 'Plugin Development' started by HackintoshMan, May 5, 2013.

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

    HackintoshMan

    I have been told to never use static items and I have been told that it is ok to use them. If I remember correctly, making something static makes it able to be used throughout the plugin/project without initializing it. Is this correct? If not what does it mean?:eek: If we should be avoiding static items, then how do we avoid it?
     
  2. Offline

    devilquak

    HackintoshMan

    Ask on your other thread. Don't split the discussion there and bring a part of it here, just keep going in your original thread.
     
  3. Offline

    Tirelessly

    Static items have their place. Whoever is telling you never to use them is wrong. However, you shouldn't make everything static just to make your life easier.
     
  4. Offline

    HackintoshMan

    I was hoping that this could help others also. The other thread has the information buried and here I can ask my question outright and start fresh for others to see.

    Ok, that helps. Eclipse always wants to make everything static! :'(

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

    Tirelessly

    Then the way you're doing things is wrong and it's trying to fix it for you.
     
  6. Offline

    chasechocolate

    If you have a static accessor method, then you will have to make the variable it accesses static.
     
  7. Offline

    HackintoshMan

    chasechocolate
    Like if the method is

    public static void main(){
    //code here
    }

    but what if the method is

    public void main(){
    //code here <-------------------- this still wants to make it static, I have tried accessing the variable differently
    }
     
  8. Offline

    Hoolean Retired Staff

    If you want to change it, do this:
    Code:java
    1. public class LolWut {
    2.  
    3. public LolWut() {
    4. //code here
    5. }
    6.  
    7. public static void main(String[] args) {
    8. new LolWut();
    9. }
    10.  
    11. }
     
  9. Offline

    HackintoshMan

    I keep getting this message:

    "cannot make static reference to non static method"

    how do I access a non static method from another class? I have the following

    Code:
    public int playerX(Player player) {
            return player.getLocation().getBlockX();
        }
    I access it in other classes by

    Code:
    if (PlayerLocation.playerX(player) ……..
     
  10. Offline

    Tirelessly

    That's because you're making a static reference to a non static method.
     
    MrBluebear3 likes this.
  11. Offline

    Digi

    HackintoshMan
    Stop for a second and think about this... you can create any number of instances of an object with 'new ClassName()', each instance has its own personal data, like each Location has diferent coordinates and worlds.

    So, by calling ClassName.method() the code can't possibly guess which instance of ClassName you're refering to, since you can create any number of them.. you need a pointer to that class... a variable.

    Static fields/methods are accessories that are available throughout all instances of that class, which also allows easy access it can also break your stuff if you don't design them properly.

    I strongly suggest you only work with instanced objects (forget about static) so you fully understand them, only then you can use static stuff because it seems you don't understand them at all and this will be a problem for you.

    Start off by removing all "static" keywords.

    Then make instances of your classes in the main class and store them in fields.... e.g. main class:
    Code:
    public SomeListener someListener = new SomeListener(this); // pass 'this' instance to the listener
    public UtilityClass utility = new UtilityClass(this);
    // etc...
    
    public void onEnable()
    {
        someListener.someMethod(); // call the method on that instance
    }
    Then in the listener you can grab the main class' instance and use any other instances from that class by simply linking them... e.g. listener class:
    Code:
    private MainClass plugin;
    
    public SomeListener(MainClass plugin) // this is the constructor, has no return type and has the class name.
    {
        this.plugin = plugin;
    }
    
    public void someMethod()
    {
        plugin.utility.someOtherMethod(); // call a method from the utility class' instance defined in the main class
    }
    You can visualize object instances like boxes in a room, each box is unique and can hold its own data and you can only grab them by pointing to them.
     
    devilquak and MrBluebear3 like this.
  12. Offline

    HackintoshMan

    Thanks, this really helped. I will start the conversion:)
     
Thread Status:
Not open for further replies.

Share This Page