0 votes
1 view
in Java by (2.3k points)

Using class GUI, I have created an object:

GUI g1 = new GUI();

 

And the reference variable:

GUI g2;

I think g2 is a reference variable that is used to reference a GUI class and g1 is an object of GUI class. So what is the difference between g1 and g2?

1 Answer

0 votes
ago by (4.4k points)

In Java, references are the names. And objects are stuff. We can provide different names for stuff, even for non-existing stuff.

We can give names, without actually giving them any real meaning, for example:

GUI g1;

You can assign meaning to names with the = operator:

GUI g1 = some_gui;

The meaning of names can be changed over time. This same name can refer to multiple things defined earlier. 

GUI g1 = some_gui;

doSomething();

g1 = some_other_gui;

Synonyms are available where one thing can have multiple names:

GUI g2 = g1;

And references are the names refer to something

We can create the stuff like:

new GUI();

Stuff can be created and named on the spot for later reference:

GUI g1 = new GUI();

And stuff can be referred to, using its name or any other:

g1.doSomething();

g2.doSomethingAgain();

We can also create different stuff of the same kind and name it differently:

GUI g1 = new GUI();

GUI g2 = new GUI();

GUI g3 = new GUI();

GUI g1_synonym = g1;

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...