Objects are like bubbles floating through a space called heap. Variables are merely links to these bubbles. The expression new GUI() creates an object and = operator links it to the variable g1. g2 is also a variable but it's not linked to any object. You can easily swap them by writing
g2 = g1;
g1 = null;
Now g2 refers to the object that g1 was referring to in the beginning, and g1 refers to nothing. The object is intact, only the variables was changed.