Solved Getting a class

Discussion in 'Spigot Plugin Development' started by MangoCodes, Oct 11, 2017.

  1. How can I get instance of my utils class? Like for example to get instance of my Main class I can simply do:

    Code (Text):

    public class Main extends JavaPlugin implements Listener {

        Main plugin;

        public void onEnable() {
            plugin = this;

        public void onDisable() {


        public Main getPlugin() {
            return plugin;
    //Other class

    Code (Text):

    public class OtherClass {

        private final Main plugin;// reference to main class

        public OtherClass(Main plugin) {
            this.plugin = plugin;
    How can I get reference\ to my utils class in my "otherclass"
    #1 MangoCodes, Oct 11, 2017
    Last edited: Oct 11, 2017
  2. In ur main class do
    private static (ClassName) instance;

    In ur onEnable put instance = this;
    In ur onDisable put instance = null;

    then make a method in your main class like
    public static (ClassName) getInstance(){ return instance}

    In ur other class u can now do MainClass.getInstance()
    • Agree Agree x 1
  3. Well your Main reference is called plugin and your trying to get instance

    Very basic Java mistakes. Just follow the code I gave u.
  4. You have to learn java... "this" keyword is for referencing the class you are in.

    What you want to do is called dependency injection.
    • Winner Winner x 1
  5. Mas


    • Agree Agree x 2
  6. 2008Choco

    Resource Staff

    It's almost like there are hundreds upon hundreds of threads with the exact same topic... I went to Google and found dozens on the front page, and these are just on SpigotMC. I could have found thousands more on Stackoverflow and various other forums. Please... look on a search engine before creating a thread. This is ridiculous
    • Winner Winner x 1
  7. First of all if your issue is solved set the thread to solved please.
    new OtherClass(this(Only put this if the class you are getting is using the main instance)).MethodYouWannaGet();
  8. Not sure if I just explained what I need wrong, but I know how to get instance of the main class. I just want to get instance of my "KitManager" class so I can access it in the "TankKit" class

    Look where I under lined, that’s one way of making an instance of ur class to get it in another class, just change the naming to your class
    • Optimistic Optimistic x 1
  10. Stop using static as an access modifier. Create a non-static getter in your plugin class which returns a non-static field, and pass your plugin instance around through the use of a constructor.
    • Agree Agree x 1
  11. See thank you, you understand what I want, Iv'e been trying to figure out a way on how I can get access to my classes without static! I understand completely on how to do it with static but it doesn't feel right in my opinion just feels like I am abusing static at that point
  12. Use parameters to pass around dependencies, f.e. in the constructor or in a method (whichever is more applicable)
    • Like Like x 1

Share This Page