Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (6.1k 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
by (11.7k 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;

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...