• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList in Object Reference Confusion

 
Caglar Cataloglu
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi dear javalovers;

I am so confused about an object reference. I am sending an object as parameter to another object`s function. Simply my 2 classes are described below...



result println should be 0 instead of 2; but result is 2; how can it be happen?
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think it should be zero? (Please provide the running code.)
Jim ... ...
 
Caglar Cataloglu
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Hoglund wrote:Why do you think it should be zero?
Jim ... ...



I am just copying the value of the arraylist in the main class. i am not trying to change it. but it just changes

If i do this.mainobject.show.add(0); inside Worker Class->print method, it will change the arraylist in the main class, but i am not doing that, and it still changes
 
Henry Wong
author
Marshal
Pie
Posts: 21515
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You need to show us the size() method of the worker class. After all, that is the method that is returning the two, isn't it?

Henry
 
Caglar Cataloglu
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
You need to show us the size() method of the worker class. After all, that is the method that is returning the two, isn't it?

Henry


oh sorry my mistake, that should be show.size(); instead of w.size(). so size method is the method of an arraylist.

 
Rajeev Rnair
Ranch Hand
Posts: 310
1
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Ok , here is the working version of code after fixing the several compile errors!
You are trying to pass a complete object of Main{} class to print() method.
And you are assigning Main.show to Worker.showed. In effect you are passing the reference of the object "showed" to "show"
So whatever changes happen to "show" will happen to "showed" also because both references point to the same "ArrayList" object.
In your case you added two Integer objects to the ArrayList via auto boxing. So the size of the list would be two in both the references.

Hope this is clear and all the best!



 
Caglar Cataloglu
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rajeev,

Thanks for your answer, i understood well. Now i tried to change a String variable in the reference to see if it will also change in the original object, but it seem doesnt change, i dont know why. Here is the working code:

 
Rajeev Rnair
Ranch Hand
Posts: 310
1
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Caglar, String's are immutable and are an exception to the above rule. "String" is an Object, but doesn't work like other Objects. When you use

it creates a new String "Changed@WorkerClass" in the pool and assign the reference to s2. The original String s="OriginalMainString"; doesn't change and will be still in the pool. Please go through JLS for String class, StringBuffer class, StringBuilder class etc

All the best!

 
Caglar Cataloglu
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajeev Rnair wrote:Hi Caglar, String's are immutable and are an exception to the above rule. "String" is an Object, but doesn't work like other Objects. When you use

it creates a new String "Changed@WorkerClass" in the pool and assign the reference to s2. The original String s="OriginalMainString"; doesn't change and will be still in the pool. Please go through JLS for String class, StringBuffer class, StringBuilder class etc

All the best!




Thanks so much!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic