1.12.2 Static abuse

Discussion in 'Spigot Plugin Development' started by Nico12, Feb 4, 2020.

Thread Status:
Not open for further replies.
  1. Hi spigot programming community, I need help since I see that in my project I am abusing the static modifier and that's why I come to ask if you can give me a solution to the static one, for example I need to use a public string from one class to another, how could I do it?
    Please do not interpret this as a spoon feeding, thanks in advance
     
  2. Getting a String from another class can be static depending on what the String is and how it is used.
     
  3. I would recomend you creating a class with all your static variables (called Constant for example) and a Util class for the methods
    Another thing you can do is put every static thing in a Util class
     
    • Like Like x 1
  4. If you have a field in one class and you need to use it in another class, you can just move the field to the other class. Problem solved! ;)

    No?

    Mind the XY problem - maybe you're asking for help solving the wrong problem. It's difficult to know because you're asking about a very specific implementation detail in a very vague/abstract way. There are really good use cases for static fields/methods (just look at java.lang.Math), but it is also very easy to accidentally slap on everything if you don't understand how to pass data around in your program. Post your code and explain what you're trying to accomplish from a high level perspective.
     
  5. You need to use every itemkey in other class?
    I think that the best would be to loop through the configurations section in the other class
    If not, you can create a list and add all the keys in the loop
     
  6. I think he's talking about
    In all case, if this is a custom object class, you can create a public function to get the string from outside
     
  7. @Nico12 It's not clear why you need to use the itemkey value(s) in another class. You're re-assigning itemkey in every iteration of the loop, so it will end up being the last key in the set. I don't think this is what you want.

    This is what I mean by the XY problem. What is the end goal you're aiming for here? This specific piece of code parses items from the config-file and puts them into a player inventory (I have some tips to improve readability later). What are you looking to do in the other class? Why do you want itemkey in the other class, and why don't you just do there what you're doing here, if you just need it to be the same?
     
  8. ^tldr

    dependency injection.
    instead of
    Code (Text):
    main {
      static string
      new events()
    }
    events {
      on event {
        main.string
      }
    }
    u do this
    Code (Text):
    main {
      new events(string)
    }
    events {
      string = null;
      events(string) {
        string = string;
      }
      on event {
        string.blah
      }
    }
    or
    Code (Text):
    main {
      string
      public getString() return string
      new events(this)
    }
    events {
      main
      events(main) {
        main = main;
      }
      on event {
        main.getString()
      }
    }
     
Thread Status:
Not open for further replies.