Using same object instance in Java between methods

时间:2016-02-12 21:05:56

标签: java oop hash oauth instance

I asked a similar question earlier today and chose an answer, but I don't think I worded it correctly to get the answer I needed. Please consider the code below..

begin

    -- tx422 <= rx422_i;
    tx422_o <= tx422;

rs422_inst:  
    rs422_top
        port map (
            rx422_i => rx422_i, -- tx422,
            tx422_o => tx422
        );
end architecture;

The object "obj" in my program is an OAuthService instance that must be used throughout the whole process without "re-creating" it (same hash, no changes).

I call to "obj" with methodA, call a function in main, then call "obj" again in methodB. When I print the hashCodes from both methodA and methodB, I get different results, telling me it is "re-creating" the instance.

How can I avoid this?

I'm sure this might sound like a stupid questions to more experienced programmers, but I'm just getting started with programming, and I can't find the solution I need anywhere. Any help will be greatly appreciated.

Actual code

Class "AuthController", where the "obj" is the OAuth10aService "service", methodA is "handlePinButton", and methodB is "handlePinSubmit". I have tried authentication with two other OAuth libraries for java, and got similar errors. Scribe-java is what I'm using now,

public class ClassA {

    private Main main;
    Object obj = new Object;

    public void setMain(Main main) {
        this.main = main;
    }
    methodA() {  //called first
        obj.someFunction();
        main.someFunction();
    }
    methodB() {  //called second
        obj.someOtherFunction();
    }
}

Outputs the following:

public class AuthController {

private final String CONSUMER_KEY = "asdbfsdfbsdf";
private final String CONSUMER_SECRET = "asdfsdbvbvfsdsd";


final OAuth10aService service = new ServiceBuilder()
        .apiKey(CONSUMER_KEY)
        .apiSecret(CONSUMER_SECRET)
        .build(TwitterApi.instance());
final Token requestToken = service.getRequestToken();

Desktop desktop = Desktop.getDesktop();

@FXML
public JFXTextField enterPinField;
@FXML
public JFXButton submitPinButton;

private Main main;

public void setMain(Main main) {
    this.main = main;
}

public void handlePinButton() throws Exception { //call first

    System.out.println(service.hashCode());
    String authUrl = service.getAuthorizationUrl(requestToken);
    desktop.browse(new URI(authUrl));
    main.secondWindow();

}

public void handlePinSubmit() throws Exception { //call second

    System.out.println(service.hashCode());

    Verifier verifier = new Verifier(enterPinField.getText());
    System.out.println(verifier);

    Token accessToken = service.getAccessToken(requestToken, verifier);
    System.out.println(accessToken);


  }
}

1 个答案:

答案 0 :(得分:1)

使用单例模式。如下所示定义您的类,并在想要获取唯一一个对象时调用MyClass.getInstance()方法而不是new MyClass()

public class MyClass
{
    /**
     * Create the only one instance of this class.
     */
    private static final MyClass sInstane = new MyClass();

    /**
     * This is the only one constructor in this class.
     * Because this is 'private', no others can create
     * instances of this class.
     */
    private MyClass()
    {
    }

    /**
     * Get the only one instance of this class.
     */
    public static MyClass getInstance()
    {
        return sInstance;
    }
}