This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes What is the reality of the term Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the reality of the term "object" and "reference" ?" Watch "What is the reality of the term "object" and "reference" ?" New topic
Author

What is the reality of the term "object" and "reference" ?

Rasul Patrick
Greenhorn

Joined: Oct 18, 2012
Posts: 20
Obtaining objects of a class is a two-step process :
First, you must declare a variable of the class type. This variable does not define an object.
Instead, it is simply a variable that can refer to an object. Second, you must acquire an actual,
physical copy of the object and assign it to that variable. You can do this using the new operator.
The new operator dynamically allocates (that is, allocates at run time) memory for an object
and returns a reference to it. This reference is, more or less, the address in memory of the object
allocated by new. This reference is then stored in the variable. Thus, in Java, all class objects
must be dynamically allocated.



This statement combines the two steps just described. It can be rewritten like this to show
each step more clearly:

The first line declares mybox as a reference to an object of type Box. After this line executes,
mybox contains the value null, which indicates that it does not yet point to an actual object.
Any attempt to use mybox at this point will result in a compile-time error. The next line
allocates an actual object and assigns a reference to it to mybox. After the second line executes,
you can use mybox as if it were a Box object. But in reality, mybox simply holds the memory
address of the actual Box object.



If mybox holds the memory address of the actual Box object then which variable is actually considered as object ?
Is I have to carry on "reference" or say "object reference variable" terms to the whole learning process of Java ?
When will I really meet an "actual object" ?
Please illustrate because before I go any further, there's something I need to be clear about.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Rasul Patrick wrote:Obtaining objects of a class is a two-step process :
First, you must declare a variable of the class type.


This is false. You can obtain and use an object without declaring a variable. For instance, System.out.println(new Date()); creates a Date object and passes a reference to it to the println() method. (Normally we will use a variable, but the statement given is not accurate.)

Second, you must acquire an actual,
physical copy of the object and assign it to that variable. You can do this using the new operator.


Note that this is one way of getting a reference to an object, but it's not the only way.

If mybox holds the memory address of the actual Box object then which variable is actually considered as object ?


None. No variable's value is ever an object in Java. The only way we access objects is through references (a.k.a. "pointers"). It's these references that get stored in variables.

Is I have to carry on "reference" or say "object reference variable" terms to the whole learning process of Java ?


It depends on the context and the audience. Among a group of developers who understand what's really happening, it's okay to get a little sloppy for the sake of brevity and say things like "passing this object to that method" or "the object in that variable." In the context of this site, however, when a beginner is asking about what gets passed or what a variable holds, it's better to be precise.

When will I really meet an "actual object" ?


Every time you dereference a pointer with the "dot" operator, for one.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
Rasul Patrick wrote: . . .
The first line declares mybox as a reference to an object of type Box. After this line executes,
mybox contains the value null, which indicates that it does not yet point to an actual object.
Any attempt to use mybox at this point will result in a compile-time error.
. . .
Not if mybox is a field. The compiler cannot check whether a field reference has an object assigned to it, so that would compile nicely and may suffer a NullPointerException at runtime. There are circumstances in which you can use a null reference.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Campbell Ritchie wrote:
Rasul Patrick wrote: . . .
The first line declares mybox as a reference to an object of type Box. After this line executes,
mybox contains the value null, which indicates that it does not yet point to an actual object.
Any attempt to use mybox at this point will result in a compile-time error.
. . .
Not if mybox is a field. The compiler cannot check whether a field reference has an object assigned to it, so that would compile nicely and may suffer a NullPointerException at runtime. There are circumstances in which you can use a null reference.


Additionally, if it's a local variable, not a field, then it won't contain null. It will not have a defined value. Any error relating to a variable holding null will come at runtime, not compile-time. And null doesn't mean it "hasn't yet" been pointed at an object, only that it doesn't currently do so. A variable can be non-null and then be set to null after that.

Really that whole explanation is just wrong.

@OP: Where did you get this passage from? Please QuoteYourSources(←click).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
I was simply picking up the one error you had overlooked. It was excusable, since there were so many errors in that original passage.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Campbell Ritchie wrote:I was simply picking up the one error you had overlooked. It was excusable, since there were so many errors in that original passage.


I had actually missed that whole chunk of the OP. Thanks for bringing it up. I don't know where that explanation comes from, but if it's any indication of how the rest of that book or website is written, it's definitely one to be avoided.
Waldemar Macijewski
Ranch Hand

Joined: Jun 22, 2012
Posts: 32
It comes from this book. Man, I have read that so many times, I though that was 100% accurate for sure. Also, I never really understanded what do you consider a "field?. Instance variable?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Okti Wmcijewski wrote:It comes from this book.

Erm...what does?

Also, I never really understanded what do you consider a "field?. Instance variable?

Well personally, I use it for almost anything: local, instance or static variables or constants. I know it's sloppy, but Java's my 6th language and I'm too old a tiger to change me stripes now, so sue me.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Okti Wmcijewski wrote:It comes from this book.


Oh, yeah. Schildt has a pretty bad reputation.

Also, I never really understanded what do you consider a "field?. Instance variable?


Yes, as far as I know, the term field means "instance variable", although I've sometimes seen it used in a way that also includes "class variable." Never seen it used to refer to locals.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
Which page?
Rasul Patrick
Greenhorn

Joined: Oct 18, 2012
Posts: 20
OK now I'm learning from this book

"Thinking in Java 4th Edition " by Bruce Eckel

Is this a right approach ?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Rasul Patrick wrote:OK now I'm learning from this book

"Thinking in Java 4th Edition " by Bruce Eckel

Is this a right approach ?


I haven't read it myself, but that book does have a solid reputation in the Java community.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
Yes, it is a good book. I have a copy myself.
Rasul Patrick
Greenhorn

Joined: Oct 18, 2012
Posts: 20
Thanks all of you for answering my question and for help and suggestions.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You're welcome! That's what we're here for.
 
GeeCON Prague 2014
 
subject: What is the reality of the term "object" and "reference" ?