1

I am looking to clarify my knowledge of java:

basketball o, s;
s = new basketball();
o = s;

The question is how many basketball objects are created, 1 or 2? I ran some tests and thought only 1 object was created, as when I modified one, it was reflected in the other. Sorry for the simple question, I was just seeking to clarify this.

Barney
  • 13
  • 2

6 Answers6

0

only one object is created but 2 referrences are created.Here(basketball o, s;) o and s are just references

But when you did s = new basketball(); then an object is created

and after that when you did o=s then you are just pointing to s but not creating any object

Have a look at this to see different ways of creating objects

Community
  • 1
  • 1
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
0

Only one object is created here. o and s are just handles to the same object and, as you said yourself, modifying one will modify the other (as they're pointing to the same object).

To understand more clearly, I'd recommend reading How is a Java reference different from a C pointer? and Is Java “pass-by-reference” or “pass-by-value”?

Community
  • 1
  • 1
blalasaadri
  • 5,990
  • 5
  • 38
  • 58
0

s is the only object created then o references/points to the location of s in memory.

Schokea
  • 708
  • 1
  • 9
  • 28
0

your variables o and s are just references, when you use the operator new AnyObject() then you create your object and it return the reference pointer to whaterver the left hand side varaible.

so you have 2 references and 1 object

and both reference variables point to that object

Wajdy Essam
  • 4,280
  • 3
  • 28
  • 33
0

Since you seem new to java i will explain in detail,

When you write this basketball o, s; in java , it means you are creating two reference variables of type basketball. What a reference variable means is , it is a type of variable that can point to the actual object of basketball.

When you write this s = new basketball(); in java it means, You are creating a new object in java , and assigning that object to the reference variable. This means you are providing a link to the object through s

Imagine this, the reference variable as a remote control and the object (instance variable) as a TV. Although all the real work is done by TV , the control of entire TV is done through the remote control. Its a link to the TV for the user.

so When you do o = s; , you are assigning the object (instance variable) pointed by s to o . Hence now both s and o point to the same object. And hence When changes are made on one it obviously will reflect on the other. Imagine it like having two remote controls for the same TV.

For your answer , Only one object is created , but two references are created.

Mustafa sabir
  • 4,130
  • 1
  • 19
  • 28
0

basketball o, s;

s = new basketball();//Here you create the object after that

o = s;//here you assign the address of 1st object(which is store in s) to o

suppose s = new basketball(); lets take the address of s is 23490.After that we put this address value into o.So o is also pointing the same object. Hence one object will be created.

BONDbATIF
  • 113
  • 1
  • 2
  • 6