• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayList in Object Reference Confusion

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 310
1
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic