r/javahelp • u/[deleted] • Jan 16 '19
[Humor] Interview with Object Reference[source: HeadFirst Java]
HeadFirst: So, tell us, what's life like for an object reference?
Reference: Pretty simple, really. I'm a remote control and i can be programmed to control different objects.
HeadFirst: Do you mean different objects even while you're running? Like, can you refer to a Dog and then five minutes later refer to a Car?
Reference: Of course not. Once I'm declared, that's it. If I'm a Dog remote control then I'll never be able to point(oops - my bad, we're not supposed to say point) I mean refer to anything but a Dog.
HeadFirst: Does that mean you can only refer to only one Dog?
Reference: No. I can be referring to one Dog, and five minutes later I can refer to some other Dog. As long as it's a Dog, I can be directed(like reprogramming your remote to different TV) to it. Unless... never mind.
HeadFirst: No, tell me. What were you gonna say?
Reference: I don't think you want to get into this now, but I'll just give you the short version - If I'm marked as final, then once I'm assigned a Dog, I can never be reprogrammed to anything else but that one and only Dog. In other words, no other object can be assigned to me.
HeadFirst: You're right, we don't want to talk into this now. OK, so unless you're final, then you can refer to one Dog and then refer to a different Dog later. Can you ever refer to nothing at all? Is it possible to not be programmed to anything?
Reference: Yes, but it disturbs me to talk about it.
HeadFirst: Why is that?
Reference: Because it means I'm null, and that's upsetting to me.
HeadFirst: You mean, because you have no value?
Reference: Oh, null is value. I'm still a remote control, but it's like your brought home a new universal remote control and you don't have a TV. I'm not programmed to control anything. They can press my buttons all day long, but nothing good happens. I just feel so...useless. A waste of bits. Granted, not that many bits, but still. And that's not the worst part. If i am the only reference to a particular object, and then i'm set to null(deprogrammed), it means that now nobody can get to that object i had been referring to.
HeadFirst: And that's because...
Reference: You have to ask? Here i've developed a relationship with this object, an intimate connection, and then the tie is suddenly, cruelly, severed. And i will never see the object again, because now it's eligible for[producer, cue tragic music] garbage collection. Sniff. But do you think programmers ever consider that? Sniff. Why, why can't i be a primitive? I hate being a reference. The responsibility, all the broken attachments...
1
5
u/[deleted] Jan 16 '19
Here's your upvote