Keeping variables straight

Discussion in 'Plugin Development' started by xTrollxDudex, Oct 11, 2013.

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

    xTrollxDudex

    Okay. So I ran into a small prob here. How would I keep a variable in each instance of an object? I made the field static but apparently it isn't working (or maybe I didn't pay too much attention to that part in Java for Dummies)

    Edit#6,968: Should I access the variable using ClassName.plugin.foo() or just plugin.foo()?
     
  2. Offline

    socram8888

    Using Static fields is wrong. If you need a "global" variable, put it in you plugin instance like this:
    Code:
    public class MahPlugin {
    	private MahListener listener;
    	public int myNumber;
    
    	public void onEnable() {
    		myNumber = 9;
    		listener = new MahListener(this);
    	}
    }
    
    Code:
    public class MahListener implements Listener {
    	private MahPlugin plugin;
    
    	public MahListener(MahPlugin plugin) {
    		this.plugin = plugin;
    	}
    
    	@EventListener
    	public void onEventFire(AnEvent event) {
    		if (event.getHeight() == plugin.myNumber) {
    			// do something
    		}
    	}
    }
     
  3. Offline

    xTrollxDudex

    *facepalm*

    No not like that. I'm using the static modifier correctly, that's not what needs to change.
     
  4. Offline

    CubieX

    There is no "instance of an object". An object is an "instance of a class".
    If you want to access this variable from other classes directly, you can use a static field.

    But just to make this clear: A (static) field is not kept in an object, but in the class itself
    and can be accessed by using "ClassName.variable".
    A static method would be similarely accessed by using "ClassName.staticMethod()".

    If this does not work for you, better post your code.
     
  5. Offline

    xTrollxDudex

    CubieX
    Alright, here's the 'gist' of it:
    Assume here's my handler, FooManager with a plugin reference:
    PHP:
    public class FooManager{
        static 
    FooManager instance = new FooManager();
        public 
    FooManager(){}

        static 
    FooPlugin plugin;
        public 
    FooManager(FooPlugin foo){
            
    plugin foo;
        }

        public static 
    FooManager getInstance(){
            return 
    instance;
        }

        public 
    void Foo(){
            
    plugin.getConfig().set("Foo"69);
        }

    }
    And use it like so:
    PHP:
    FooManager.getInstance().Foo();
    This would (should) throw a NullPointerException for 2 reasons:
    1) plugin is null
    2) probably an IllegalArgumentException but the key "Foo" does not exist

    The question is: how can I keep the field 'plugin' in initialized in each instance of FooManager? I'll keep trying different methods.
     
  6. Offline

    Syd

    xTrollxDudex
    You could use a FooPlugin#getInstance() as well.
     
  7. Offline

    CubieX

    Weird structure.

    You have defined an overloaded constructor.
    So I guess you are instantiating FooManager at some point in the calling class.
    But you are also creating a static instance of FooManager without giving parameters for the constructor in FooManager class itself.
    So you are creating a second instance of FooManager that has no reference to "plugin".
    If you now use "FooManager.getInstance()" you will receive this second instance of FooManager which has no initialized "plugin" variable.

    Two possibilities:
    1. Make the whole FooManager class "static". So you do not need to instantiate it.
    But you said you need to instanciate it. So this is not the way to go.
    2. Instantiate FooManager wherever you need it and always pass a reference to your main class to it's constructor.
    Delete the line with the static instance creation of FooManager.
     
  8. Offline

    xTrollxDudex

    That's exactly what I was trying to avoid. Are you sure there are not alternatives?
     
  9. Offline

    iZanax

    xTrollxDudex

    Try to search on 'singleton'
    That is the term used u are looking for.
    I don't got experience with it, So I can't help u further.
    But it will hopely will explain how it get it to work.

    EDIT: I'm not sure if there is a way with a constructor with attributes..
     
  10. Offline

    xTrollxDudex

    iZanax
    I already know what singletons are...
     
  11. Offline

    iZanax

    xTrollxDudex
    Okay why don't you research on that?

    Singletons, don't have a constructor with variables...
    What I can suggest is to have 1 Object in your Handler/Controller/Main and get the instance due that class.

    #Handler.getFooManagerInstance()#??
     
  12. Offline

    xTrollxDudex

    iZanax
    Say that the FooManager needs the plugin variable in each of its instances. If I had to initialize it every time it used the manager, then I'd defeat the entire purpose of having one.
     
  13. Offline

    Techcable

    xTrollxDudex
    if you want arguments to the constructor you should probably just use a normal class w/ just one reference you pass around so you can make a new one if you want a different set of arguments
    basic singleton:
    Code:java
    1. public class Singleton(
    2. //make sure you initiate it in getInstance() if you want to pass args to the constructor
    3. private static Singleton instance;
    4. private Object arg;
    5. //keep the constructor private!!!
    6. [U]private[/U] Singleton (Object arg) {
    7. this.arg = arg;
    8. }
    9. //use the getInstance method as you would a constructor
    10. public static Singleton getInstance(Object arg) {
    11. if(instance == null) instance = new Singleton(arg);
    12. return instance;
    13. }
    14. )
     
  14. Offline

    xTrollxDudex

    Techcable
     
  15. Offline

    Cirno

    I don't understand much of what you're saying, but, if it's about global plugin access, this is what I do for my core plugin:
    Code:java
    1.  
    2. private static MahouTenkoder instance;
    3.  
    4. public void onEnable(){
    5. instance = this;
    6. }
    7.  
    8. public void onDisable(){
    9. instance = null;
    10. }
    11.  
    12. public static MahouTenkoder getPlugin(){
    13. return instance;
    14. }
     
  16. Offline

    xTrollxDudex

    Last edited by a moderator: Jun 4, 2016
Thread Status:
Not open for further replies.

Share This Page