Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Object" vs "Object Variable" in Java?

Tags:

I am teaching myself Java, and one of the review exercises in my book asks for the difference between an "object" and an "object variable."

I know what an object is (a specific instance of a class), but I can't seem to find the term "object variable" anywhere in the book (no answers section) or on the internet.

Does anyone know the meaning of this term?

like image 319
Faaris Cervon Avatar asked Jul 02 '13 18:07

Faaris Cervon


2 Answers

I'll bite.

The Object is the instance itself, whereas the Object Variable is the reference to the Object.

Here's a contrived example:

Object o = new Object(); Object ref1 = o; 

In his case, there is a single instance of the Object, but it is referenced by two Object Variables: o and ref1.

When an Object is no longer referenced by an Object Variable, the Object is garbage collected.

like image 157
Will Hartung Avatar answered Oct 07 '22 04:10

Will Hartung


It's a synonym of "instance variable":

class A {     static int m;  // <-- class variable     int n;         // <-- instance variable     ... } 

Evidently, this term is not so commonly used, and it would better to avoid any potential ambiguities by just sticking with "instance variable".

like image 29
arshajii Avatar answered Oct 07 '22 04:10

arshajii