5

I'm developing a GUI program, where I have made classes, that cluster ActionListeners, by functionality. My question is regarding how the JVM handles jButtons, that has the same ActionListener added to them.

First; I am aware that the JVM can save memory, by letting two reference variables that point to an identical string (for instance), point to the same string object in the memory.

public class Example {
    String str1 = "SomeString";
    String str2 = "SomeString";  
}

Now, my question is this: If I have, say, 5 jButtons. All buttons have the same ActionListener added to them. When the program is run, will they have 5 seperate, identical, instaces of the same class added to them? Or will the JVM do something similar (to the above mentioned) ?

  • Thanks in advance :)
4
  • If you show us what you're doing we can tell you what will happen. Or you can test it out for yourself. Your question is confusing - if you pass the same ActionListener object to five different buttons, why would you think it would split into five objects? Commented Aug 23, 2013 at 18:56
  • Thanks for your response. In my example, I meant; if I explicitly add five different object to five different buttons - will the JVM optimize memory space, and let them share the same object. Commented Aug 23, 2013 at 19:21
  • In that case, no. The JVM does not merge objects like that. Strings are a special case. You could, however, use the same ActionListener for multiple buttons. Commented Aug 23, 2013 at 19:24
  • Thanks a lot! that perfectly answer my question! Commented Aug 23, 2013 at 19:29

3 Answers 3

4

Well, it really depends on how you created the ActionListeners. If you did

button1.addActionListener(new ActionListener() {
    ....
});
....
button5.addActionListener(new ActionListener() {
    ....
});

Or

ActionListener al= new ActionListener() {
    ....
};
button1.addActionListener(al);
....
button5.addActionListener(al);

In the first case you, true, have 5 different action listeners. But in the second you have only one. When can you have only one? When it does exactly the same and on the same objects.

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

3 Comments

The first would be separate objects, and the second would be the same object, correct?
Thanks for answering. I have been doing what you mention in your first example - and now that you mention the last example, Im surprised I didn't do that. But as shown in my question example; my thoughts was mainly whether the JVM could pick up, that a number of jButtons have been given identical objects, and hence let them then share the same.
That's a very good question. And the answer is not easy. Perhaps just not smart enough? Leaving some work for us to do?
3

It depends.

This will give them the same instance.

ActionListener al = new ActionListener() { ... };
button.addActionListener(al);
button2.addActionListener(al);
...

while this will give them their own.

button.addActionListener(new ActionListener() { ... });
button2.addActionListener(new ActionListener() { ... });

Comments

1

I believe it would pass the same ActionListener object to all 5 buttons. If you want to know the truth, I suggest you test it for yourself

Comments

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.