0

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?

7
  • 2
    Maybe it's me, but I don't understand your problem at all, and the posted code does not help me. Please clarify your problem more for us. Consider creating and posting an sscce. Commented Aug 3, 2013 at 13:13
  • 1
    @HovercraftFullOfEels not just you m8 Commented Aug 3, 2013 at 13:28
  • 2
    after you pass the hashset to classtwo, does classone modify it again (maybe call clear())? Commented Aug 3, 2013 at 13:44
  • @jtahlborn's deleted answer may have identified your problem. Again, please clarify. Commented Aug 3, 2013 at 13:53
  • @HovercraftFullOfEels Just updated to try and clarify. Commented Aug 3, 2013 at 18:22

2 Answers 2

2

i expect that after you pass the hashset to classtwo, classone is modifying it again (perhaps calling clear()).

Sign up to request clarification or add additional context in comments.

6 Comments

This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question.
@Rubens - maybe should read the whole question and comments before commenting. you can consider my question rhetorical.
@Sergio - ditto what i said to Rubens
@jtahlborn Long as this is a question -- rhetorical or not --, I found fine to add the comment. Yet, if you think your rhetorical question suffices as an answer, instead of a comment in the OP's post, I drawback my comment.
@Rubens - did you read the entire question, comments and other answer?
|
1

You state:

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?

The answer to this was likely given already an answer posted and deleted by jtahlborn.

Since you know for a certainty that the Class2 instance is being created with a HashSet that is filled with pertinent data, and later see that the data is gone, this can only mean as jtahlborn suggests, that you're deleting the data from the HashSet after you create the Class2 instance.

The key concept to understand is that when you pass in your HashSet parameter into the Class2 constructor, you are passing in a reference to the original HashSet that resides in Class1. Any changes made to the original object will be seen in all references to that object. If you don't want this to happen, then you need to pass in a copy of the HashSet.

Try doing:

classTwo = new classTwo(new HashSet<String>(extensions));

Since Strings are immutable, there's no need to do a deeper copy than this. The same isn't true if your HashSet contains mutable objects.

1 Comment

@jtahlborn Thanks! I was calling clear() afterwards. I guess I was thinking that it wouldn't be called until the operations in the run() method were complete. Doh!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.