What is the difference between a name of an object and a reference toan object?

K

Knute Johnson

tenxian said:
Can anyone give me a clarification about both?

From the JLS -

"Names are used to refer to entities declared in a program. A declared
entity (§6.1) is a package, class type (normal or enum), interface type
(normal or annotation type), member (class, interface, field, or method)
of a reference type, type parameter (of a class, interface, method or
constructor) (§4.4), parameter (to a method, constructor, or exception
handler), or local variable.

Names in programs are either simple, consisting of a single identifier,
or qualified, consisting of a sequence of identifiers separated by "."
tokens (§6.2).

Every declaration that introduces a name has a scope (§6.3), which is
the part of the program text within which the declared entity can be
referred to by a simple name.

Packages and reference types (that is, class types, interface types, and
array types) have members (§6.4). A member can be referred to using a
qualified name N.x, where N is a simple or qualified name and x is an
identifier. If N names a package, then x is a member of that package,
which is either a class or interface type or a subpackage. If N names a
reference type or a variable of a reference type, then x names a member
of that type, which is either a class, an interface, a field, or a method."

and for references

"There are three kinds of reference types: class types (§8), interface
types (§9), and array types (§10). Reference types may be parameterized
(§4.5) with type arguments (§4.4)." ...

"The reference values (often just references) are pointers to these
objects, and a special null reference, which refers to no object."
 
S

Stefan Ram

Patricia Shanahan said:
a reference can be stored in a variable or field that does have a name.
Nitpick: A non-null reference is a pointer to an object.

Nitpick: A field /is/ a variable, so there is no need
for »variable or field«.

Variables are typed storage locations.

»A variable is a storage location and has an associated type«

JLS3, 4.12

http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.12

There are variables without names: The components of an array.
 
D

Daniel Pitts

Lew said:
Variables (sometimes) have names; objects don't.
public class MyNamedObject {
final String name;
MyNamedObject(String name) { this.name = name; }

public static void main(String...args) {
System.out.println(new MyNamedObject("Bob"));
}

public String toString() {
return "Lew was wrong, my name is " + name + ".";
}
}

:) Objects don't have intrinsic names.
 
L

Lord Zoltar

Variables (sometimes) have names; objects don't.

What about objects that are referenced through JNDI? Or maybe CORBA?
I've always thought of them as "named objects"...
 
T

tenxian

(Referencing: What is the difference between a name of an object and a
reference to an object?)

In Java, objects do not have names. A reference is a pointer to an object.

Objects in Java do not have names. Only classes in Java have names. So
if we want to mention an object, what can we do? To mention its
reference? Why can't we consider its reference as its name? I can't
tell the difference between an object's name and its reference.
 
R

RedGrittyBrick

tenxian said:
Objects in Java do not have names. Only classes in Java have names. So
if we want to mention an object, what can we do? To mention its
reference? Why can't we consider its reference as its name?

Firstly because an object may have many references or it may have none
(waiting for GC).

Secondly because a reference is a pointer, so it doesn't have the same
characteristics as a name. A reference might be something like
0xd7f445a39. Instead of "reference" I guess you mean "the name of a
variable which holds a reference (to an object)" but that won't do since
with something like "List<String> frodo" the String object that
contains "ring" is only one of the many objects ephemerally referred to
via one of the values of the variable named "frodo".

I can't
tell the difference between an object's name and its reference.

http://tinyurl.com/3aefks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top