+ Reply to Thread
Results 1 to 2 of 2

Thread: Is getApi() static?

  1. #1
    Senior Member
    Join Date
    Sep 2007
    Posts
    142
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Is getApi() static?

    I have a class which is not derived from a Plugin. This class needs access to the PluginApi. I'm wondering if I could just set a static variable in the class with the value of getApi() obtained from the main BasePlugin class so that I don't have to always pass getApi() in as a parameter. In other words, does getApi() always return the same value or can it change over the lifetime of the extension?

    Code:
    public class Main extends BasePlugin {
    	@Override
    	public void init(EsObjectRO parameters) {
    		// This?
    		OtherClass.api = getApi();
    		OtherClass.getRooms1(1);
    		
    		// Or this?
    		OtherClass.getRooms2(getApi(), 1);
    		
    	}
    }
    
    public class OtherClass {
    	public static PluginApi api;
    	
    	static void getRooms1(int zoneId) {
    		Collection<RoomValue> rooms = OtherClass.api.getRoomsInZone(zoneId);
    	}
    
    	// Or is this needed
    	
    	static void getRooms2(PluginApi api, int zoneId) {
    		Collection<RoomValue> rooms = api.getRoomsInZone(zoneId);
    	}
    }

  2. #2
    Administrator tcarr's Avatar
    Join Date
    Dec 2007
    Posts
    7,287
    Thanks
    81
    Thanked 1,094 Times in 1,083 Posts
    getApi is not a static method. The api variable is injected into the plugin instance when the plugin is created. This means that if you want a static class that uses an api variable, it will have to be passed with the method call. If you create an object class, you can store the api value in the class.

    edit: the value returned by getApi() for a given plugin will not change over the life of that plugin, if that helps. I may have misunderstood your question.
    Teresa Carrigan
    Senior Engineer
    Electrotank, Inc.

  3. The Following User Says Thank You to tcarr For This Useful Post:

    ritty (08-26-2011)

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts