Java, reference variables that point to the same object in the memory -


i'm developing gui program, have made classes, cluster actionlisteners, functionality. question regarding how jvm handles jbuttons, has same actionlistener added them.

first; aware jvm can save memory, letting 2 reference variables point identical string (for instance), point same string object in memory.

public class example {     string str1 = "somestring";     string str2 = "somestring";   } 

now, question this: if have, say, 5 jbuttons. buttons have same actionlistener added them. when program run, have 5 seperate, identical, instaces of same class added them? or jvm similar (to above mentioned) ?

  • thanks in advance :)

well, depends on how created actionlisteners. if did

button1.addactionlistener(new actionlistener() {     .... }); .... button5.addactionlistener(new actionlistener() {     .... }); 

or

actionlistener al= new actionlistener() {     .... }; button1.addactionlistener(al); .... button5.addactionlistener(al); 

in first case you, true, have 5 different action listeners. in second have one. when can have one? when same , on same objects.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -