I'm working on passing data between two classes. In ClassOne, I build a HashSet bashed on user input from a GUI that I then want to pass to ClassTwo by reference.
The problem is I the variable is empty by the time my methods get run, but not in the constructor. Below is the simplified version of what I'm doing.
ClassOne
public class ClassOne
{
HashSet<String> extensions;
// run this when the button is clicked
class startListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// run the method to populate the hashset
parseExt();
// create a new class passing HashSet
classTwo = new classTwo(extensions);
}
}
// populates HashSet
public void parseExt()
{
extensions = new HashSet<String>();
extensions.add("foo");
extensions.add("bar");
extensions.add("bat");
}
}
ClassTwo
public class ClassTwo implements Runnable
{
HashSet<String> extensions;
public ChgOwner(HashSet<String> extensions)
{
this.extensions = extensions;
// prints [foo, bar, bat]
System.out.println(this.extensions);
}
public void run()
{
// prints []
System.out.println(this.extensions);
}
Thoughts on what I'm doing wrong?
=======
EDIT: To try and clarify, the variable extensions is being passed from ClassOne to ClassTwo through ClassTwo's constructor. In the constructor, I'm assigning the value to the property in ClassTwo, also named extensions. In ClassTwo's constructor, I can print the values of extensions with a result of "[foo, bar, bat]". But when printing the value in the run() method in ClassTwo, the HashSet is empty and outputs "[]".
My question is why is ClassTwo's "extensions" property empty in the method, but not in the constructor?
clear())?