Solved Global variable

Discussion in 'Spigot Plugin Development' started by Pilottt, Jun 26, 2016.

  1. Hello,

    How could my other classes access the "chat" variable at the main class?

    Code (Text):
    //Main Class

    public class Main{

        public boolean chatVariable;

    }
    Thanks.
     
  2. public static boolean ChatVariable will help
     
  3. 4chan

    Supporter

    Code (Text):
    public class Main
    {
        private static Main instance;
        private boolean chatVariable;
        public static Main getInstance() { return instance; }
        public void onEnable() { instance = this; }
        public boolean getChatVariable() { return chatVariable; }
    }

    public class OtherClass
    {
        public void someMethod() { return Main.getInstance().getChatVariable(); }
    }
    Wrote the code on the site, should work, untested though, and I believe it's better than @EnervateD's solution.
     
    • Like Like x 1
  4. @4chan Why you think making boolean static is worse than your code?
     
  5. 4chan

    Supporter

    1. Conventions
    2. Static Abuse (to the readers, yeah, I learnt)
     
  6. @4chan

    So why can't we figure it our like this?:

    Code (Java):
    private static boolean chatVariable;

    public static boolean getChatVariable() {
    return chatVariable;
    }
     
  7. 4chan

    Supporter

    Stateeek abooose.
    (Static Abuse)

    Hell, I could be wrong, but I am sure that what you're doing is static abuse.
     
  8. Because Java is an object-oriented programming language. Making your variables static violate those principles. Global variables should only be used in the moment when they can also be declared final - in settings classes or as translatable Strings. OP should learn how to use encapsulation.

    Singleton pattern is also not a perfect way to work around your objects, you can read about it here:
    http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons
    and here:
    https://jorudolph.wordpress.com/2009/11/22/singleton-considerations/

    Just pass your plugin instance in the objects you create.

    It doesn't even make any sense.
     
    • Like Like x 1
  9. @4chan
    Thanks, will mention it in my code!
     
    • Like Like x 1
  10. How do I pass the instance and get the variable?

    Thanks.
     
  11. New class:
    Code (Java):
    public class NewClass {

        private Main pluginVariable; // variable in which we store the object we pass through here

        public NewClass(Main plugin) { // use the constractor to pass the object
            this.pluginVariable = plugin; // save the passed object in a variable
        }

        public void method() {
            pluginVariable.getLogger().info("now we can access the getLogger() method inherited from JavaPlugin in classes we pass the main instance through");
        }
    }
    'pluginVariable' and 'plugin' can be named the same - especially if you use the 'this' keyword. I often use it just as
    Code (Java):
    this.plugin = plugin;

    Main Class:
    Code (Java):
    public class Main extends JavaPlugin {

        @Override
        public void onEnable() {
            new NewClass(this); // we create the object and pass the instance of the main class using the 'this' keyword.
        }
    }
    Not entirely sure if the code is compilable at this moment, I've just written it on the fly. There could be some issues.

    Also note: I've used the Main class as an example, you can use this method with any class you wish.
     
    • Like Like x 1
  12. Thanks!

    Solved.
     
  13. 4chan

    Supporter

    I personally hate passing around instances of things.
     
    • Optimistic Optimistic x 1
  14. How can I access and edit the "chat" Variable with that method?

    Thanks.
     
  15. You create a getter and setter.
     
    • Agree Agree x 1