How to use a getInstance() method for a Main class?

Discussion in 'Plugin Development' started by TheWolfBadger, Jul 19, 2014.

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

    TheWolfBadger

    I was wondering on how to use a getInstance method for the main class. I kept making a variable in each class like "Main m;" and I would do m.getInstance() and would get a NullPointerException. My question is is the best way to go about this is by making the method static and running Main.getInstance() every time I want the main class or what? Help is appreciated. Thank you very much. - Jack Badger
     
  2. Offline

    Habbolant

    Your main class:
    Code:
    public class MyMain {
    private static MyMain instance;
     
    @Override
    public void onEnable() {
    instance = this;
    }
     
    public static MyMain getInstance() {
    return instance;
    }
     
    public void myMethod() {
     
    }
     
    }
    Other class:
    Code:
    public class Other {
    private MyMain main = MyMain.getInstance();
    main.myMethod();
    }
     
  3. Offline

    xTrollxDudex

  4. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    TheWolfBadger The better solution is to not create a static getInstance method in your main class. All objects in your plugin are created from your plugin instance, so you can pass your plugin instance to their constructors.
     
    Zupsub likes this.
  5. Offline

    Traks

    Best thing to do is passing the current instance of your main class to other classes that require it by using a constructor. Passing objects is object orientated programming, using statics isn't in my opinion, and Java is an object orientated programming language. Why use statics when you don't have to? They only introduce a more interdependent class system.
    Code:java
    1. public class SomeClass {
    2.  
    3. private Main main;
    4.  
    5. public SomeClass(Main main) {
    6. this.main = main;
    7. }
    8. }
     
  6. Offline

    TheWolfBadger

    mbaxter What if I have a custom event I made. I need to use the Main in that custom event class so I can get the config. How would I pass it to an event class though?
     
  7. Offline

    Traks

    With the example I provided, just create a new instance of SomeClass (which could implement Listener) using
    Code:java
    1. new SomeClass(this)

    in your main class (in this case Main)
     
  8. Offline

    TheWolfBadger

    Traks I mean a class that extends Event not just implementing a listener...
     
  9. Offline

    xmarinusx

    You can still add your main class object to the constructor.
     
  10. Offline

    Gater12

    TheWolfBadger
    Create constructor to accept main class instance.
     
  11. Offline

    TheWolfBadger

    xmarinusx What if someone calls the event like "new EventName(Params in here)" wouldn't the Main then be null bc it's not the exact same instance or am I wrong?
     
  12. Offline

    Gater12

    TheWolfBadger
    Why would they fire it? The general logic is you fire it and some developer listens on it to do a specific function when that event fires.
     
  13. Offline

    TheWolfBadger

    Gater12 It's an API I'm making, so they would be firing it and not me.
     
  14. Offline

    xmarinusx

    It depends, if you're going to use the events in other plugins that could case trouble. It's still possible though, like how you would use any othe API in your plugin.

    Why exactly do you need things from your main class in that event then?

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

    TheWolfBadger

Thread Status:
Not open for further replies.

Share This Page