Results 1 to 6 of 6
  1. #1
    veronique is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

    Default 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,

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,317
    Rep Power
    25

    Default 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 token
    The standard way for a second class to get the value of a variable in a class is by having a getter method in the class with the variable that returns the value of the variable to the caller.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,523
    Rep Power
    5

    Default 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,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    veronique is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

    Default 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.

  5. #5
    veronique is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

    Default 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?

  6. #6
    veronique is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

    Default 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

  1. Can the class be instantiated with the static keyword ?
    By kashyapkeshav in forum New To Java
    Replies: 2
    Last Post: 11-04-2012, 01:57 PM
  2. Using variables from a non-instantiated class
    By Skater901 in forum New To Java
    Replies: 4
    Last Post: 05-19-2012, 12:43 PM
  3. Class can't be instantiated.
    By 3maksim in forum Java Applets
    Replies: 12
    Last Post: 01-25-2012, 05:23 AM
  4. Replies: 5
    Last Post: 10-11-2010, 01:16 AM
  5. class is abstract; cannot be instantiated
    By Jamison5213 in forum New To Java
    Replies: 3
    Last Post: 04-25-2010, 09:16 PM

Posting Permissions

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