Solved Getting a class

Discussion in 'Spigot Plugin Development' started by MangoCodes, Oct 11, 2017 at 2:51 PM.

  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;

        @Override
        public void onEnable() {
            plugin = this;
        }

        @Override
        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 at 2:51 PM
    Last edited: Oct 11, 2017 at 2:57 PM
  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
    • Agree Agree x 2
  5. 2008Choco

    Resource Staff

    https://www.spigotmc.org/threads/accessing-methods-in-another-class.60646/
    https://www.spigotmc.org/threads/best-way-to-get-instance.17776/
    https://www.spigotmc.org/threads/main-class-access-methods.166183/
    https://www.spigotmc.org/threads/instance-main-class.235738/
    https://www.spigotmc.org/threads/getconfig-outside-main-class.117854/
    https://www.spigotmc.org/threads/newb-pipe-main-class-functions-to-secondary-class.139048/

    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
  6. 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();
     
  7. 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
     
  8. http://prntscr.com/gwasyz

    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
  9. 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
  10. 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
     
  11. 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