tags:

views:

374

answers:

3

So lets say that in my entry point class (i.e the class which runs when the program starts (which has the public static void main(String args[]) function). In that class I have this variable:

private ArrayList<String> myData=new ArrayList<String>();

This class instantiates another class, which needs to have access to the myData member of the entry point class. How can it retrieve this arraylist?

Edit: To clarify, in the main() method I could do this:

SomeOtherClass myClass=new SomeOtherClass();

and then I could do:

myClass.someMethod();

however, in the myClass object, how could I perform a method/retrieve something from the entry class, which instantiated the myClass object?

+1  A: 

The class containing main() is just an ordinary class. In your case, you'd have to make myData public and possibly static (or, of course, add an accessor). Just like you'd do with any other class.

You could also pass an Entry object to the other class, like this:

public static void main(String[] args) {
    Entry entry = new Entry();
    SomeOtherClass myClass=new SomeOtherClass(entry);
    // continue as before
}
Michael Myers
Encapsulation matters!
banjollity
Yep. The question is why the ArrayList is in the entry class if it's being used somewhere else, but I'm not addressing that.
Michael Myers
+2  A: 

The best way to give the class you instantiate access to myData would be to pass it into the constructor when it is created.

Then, in your constructor, you can save the ArrayList into a member variable of the class.

For example, your object constructor will look like:

private ArrayList<String> myData;

public YourObjConstructor(ArrayList<String> data){
    myData = data;
}
jjnguy
+2  A: 

It sounds like your entry point is still static when it calls some other class, but your ArrayList is a member of an instance of it. You need to move out of the static world and into instances.

I'd refactor your main method into a private construtor, and put in a new main() which launches it as a new instance.

Note that this code is very rough, but it should serve to illustrate what you need to do.

public class EntryPoint {
    private ArrayList<String> myData=new ArrayList<String>();

    public static void main( String[] args ) {
        EntryPoint ep = new EntryPoint();
        ep.init();
    }

    private void init() {
        // Populate myData perhaps?

        SomeOtherClass myClass=new SomeOtherClass();
        myClass.someMethod( this );
    }

    public List<String> getMyData() {
        return myData;
    }
}   

public class SomeOtherClass {
    public void someMethod( EntryPoint entry ) {
        List<String> data = entry.getMyData();
        // do stuff with data..!
    }
}
banjollity
I would generally avoid calling someMethod() in the constructor of EntryPoint, personally. Maybe add a doWork() method or something?
Michael Myers
Well yes. I did say it was rough!
banjollity
Actually you cannot safely pass "this" from a constroctor as the object has not been fully created yet. This can lead to hard to track down bugs. It _might_ work in this case but there are many other cases where it is broken.
TofuBeer
Well yes. I did say it was rough! I've added an init() method though. Thanks.
banjollity
much better. we once spent the better part of a week tracking something down because of using "this" from a constructor (nasty threading bug).
TofuBeer