Results 1 to 6 of 6
- 03-19-2014, 02:26 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 22
- Rep Power
- 0
How to inject a variable that is instantiated in one class into another
Let's say I have a Junit4 class FooTest.java and variable token. At some point in class the token gets instantiated.
Java Code:public class Foo { private String token; @Before public void setUp() { // serious is steps to get the application to a certain state where token can get extracted .... token = extractToken(); } }
Now I have another class User.java where I need to use this token. Can I inject this token somehow into that class? There is no relation between Foo and User. I can't use Provider method in the configure file because extraction of the token depends on certain system's state and can't be called anywhere anytime.
thanks,
- 03-19-2014, 02:38 AM #2
Re: How to inject a variable that is instantiated in one class into another
have another class User.java where I need to use this tokenIf you don't understand my response, don't ignore it, ask a question.
- 03-19-2014, 02:42 AM #3
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: How to inject a variable that is instantiated in one class into another
Clearly the User class must have access to the Foo class. Otherwise, it can't get the token. So you should from User class, instantiate Foo to do the setUp and then call a fooInstance.getToken() method to get the token. Or User must have access to some other class where it can get the instance of Foo class. So then the User class calls otherInstance.getFoo().getToken() to get the token.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 03-19-2014, 02:46 AM #4
Member
- Join Date
- Apr 2011
- Posts
- 22
- Rep Power
- 0
Re: How to inject a variable that is instantiated in one class into another
Yes but this is a different situation. Foo is a test class. User.java shouldn't be calling any methods from test class. Or even referencing static variables from it. Consider User.java a supporting utility for test classes.
- 03-19-2014, 02:48 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 22
- Rep Power
- 0
Re: How to inject a variable that is instantiated in one class into another
can I used an injector in Foo?
Injector injector = Guice.createInjector(token);
and then get it in User?
- 03-19-2014, 02:57 AM #6
Member
- Join Date
- Apr 2011
- Posts
- 22
- Rep Power
- 0
Re: How to inject a variable that is instantiated in one class into another
In a traditional way I could do
new User(token); from any class that extends Foo.java and it would work. How can I do the same with Guice?
I get the part of creating the constructor in User.java that takes a token as param. Also I annotated it with @Inject.
Now when I inject @Inject User user; into any of the classes that extend Foo I get null. Not sure what else needs to be done? Bind token in configure?
Similar Threads
-
Can the class be instantiated with the static keyword ?
By kashyapkeshav in forum New To JavaReplies: 2Last Post: 11-04-2012, 02:57 PM -
Using variables from a non-instantiated class
By Skater901 in forum New To JavaReplies: 4Last Post: 05-19-2012, 01:43 PM -
Class can't be instantiated.
By 3maksim in forum Java AppletsReplies: 12Last Post: 01-25-2012, 06:23 AM -
accessing variable/methods of a instantiated object
By hariza in forum AWT / SwingReplies: 5Last Post: 10-11-2010, 02:16 AM -
class is abstract; cannot be instantiated
By Jamison5213 in forum New To JavaReplies: 3Last Post: 04-25-2010, 10:16 PM
Bookmarks