user405398
user405398

Reputation:

How to access an object which is created in some method, from different method?

I want to access an object which is created in some method of a class, from other method of same class.

public class SomeClass implements SomeInterface{

public void someMethod(int a, int b){ //Implemented method
{
 SomeOtherClass soc = new SomeOtherClass();
//some setting process
}

public int someOtherMethod(){
SomeOtherClass newSOC = soc;//Here, can i access the soc object created in "someMethod"
}
}

Actually, i want to set some data in the object and retrieve the data from different method. Is that possible!!!

Upvotes: 0

Views: 608

Answers (5)

Nathan Hughes
Nathan Hughes

Reputation: 96394

Return it from the method that creates it. Or make the object an instance member of the class. Hard to say without knowing the context.

There are some things that ThreadLocal is good for (examples are request authorization information, connection and transaction in Spring), but it could also be a very dirty hack. It would be preferable to pass information explicitly through parameters so it is clearer what is going on.

Upvotes: 2

mhaller
mhaller

Reputation: 14232

Given the constraints that the object instanced of SomeClass is used for multiple requests (e.g. where one request are both invocations of the two methods) and that they must be encapsulated properly, e.g. in a multi user system, you need to have a state object where you can transfer the results of the first method to the second method.

Since the object instance of SomeClass is considered a shared class in this scenario, you need something else.

Two ways:

  • Introduce a state object which you can pass from the first method to the second method (e.g. make the first method return the object and pass it as a parameter to the second method)
  • Introduce an internal state which is not visible to other callers. This may be risky if you do not know what you are doing and it heavily depends on the context in which the object is being used.

Both options require to change the implementation of the first method.

If you cannot, for any reason, change the implementation of the first method, the answer to your question is: No, you cannot access the soc object from with the second method, as the first object is out of scope, e.g. it may already be garbage collected and gone.

For the latter, you may want to use a ThreadLocal. This comes in handy if you know that a request (for example a HTTP Servlet Request) is unique for a user or a request and so you can share the information/state object in a ThreadLocal between the invocation of the first method and the second method. If you object instance of SomeClass is reused by different Threads, then you need to take care of properly cleaning up!

Upvotes: 1

Telmo Marques
Telmo Marques

Reputation: 5106

If you want an object to be accessed by all methods of a class, then I'd say that zOObs's approach it's the correct one. By declaring a private field in a class, that field will be visible by all methods inside that class.

This code is z00bs's approach, with just some more details:

public class SomeClass implements SomeInterface {
    //Here we declare the private field, visible by all this class. It's value is null
    private SomeOtherClass fSoc;

    public void someMethod(int a, int b) {
       /*Because we declared a private field named fSoc, it can be accessed by all methods of this class.
       Here we assign it a new instance of another class. It's value is no longer null.*/
       fSoc = new SomeOtherClass();
    }

    public int someOtherMethod() {
        /*In this method access the fSoc field, that already has been initialized by someMethod().
        But as z00bs's said, keep in mind that if someMethod() wasn't already called, then fSoc isn't initialized (it's value could be null)*/
        if(fSoc != null)
            //If fSoc is not null, do something...
            String someVariable = fSoc.someMethod();
    }
}

Upvotes: 0

z00bs
z00bs

Reputation: 7498

By declaring a field and assigning the newly created object to it or by passing the created object to 2nd method.

Assigning a field:

public class SomeClass implements SomeInterface {
    private SomeOtherClass fSoc;

    public void someMethod(int a, int b) {
       fSoc = new SomeOtherClass();
       fSoc.setSomeField("w000t");
    }

    public int someOtherMethod() {
        // access here the field fSoc but keep in mind
        // that it might not have been initialized
        if (fSoc != null) {
            String someData = fSoc.getSomeField();
        }
    }
}

public class SomeOtherClass {
    private String fSomeField;

    public void setSomeField(String field) {
        fSomeField = field;
    }

    public String getSomeField() {
        return fSomeField;
    }
}

Upvotes: 0

Eternal Noob
Eternal Noob

Reputation: 2807

Making a global object, and making that object refer to newly created object, so can any method can access it.

i.e.

public Object global_object;

void a(){

Object aObj = new Object();
global_object = aObj;


}


void b(){
//use global_object here.
}

Upvotes: 0

Related Questions