Anon0441
Anon0441

Reputation: 67

Object and reference creation

If you have

Student stud1 = new Student();

does that create both a reference and an object of type Student? Would stud1 be the reference of object Student?

Upvotes: 0

Views: 51

Answers (1)

user10762593
user10762593

Reputation:

does that create both a reference and an object of type Student?

new Student() creates an object. That creation procedure now has to be able to communicate to its caller what it has created, because after all it's useless to create a thing and not be able to tell you about the thing.

The way in Java to talk about this sort of "what" is called a "reference". So new creates an object and returns a reference to it. There's really nothing else it could do.

Student stud1 declares a variable that holds a reference to some object of type Student. The = operator stores the reference returned from new into stud1.

Saying that a reference is "created" as well as the object seems to be making heavy work of the truism that in order to refer to a thing you need to have a reference to it. Being able to refer to a thing is surely part of thing-ness. If there is no reference to a thing, there is effectively no thing.

Upvotes: 4

Related Questions