File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ArrayList in Object Reference Confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ArrayList in Object Reference Confusion" Watch "ArrayList in Object Reference Confusion" New topic
Author

ArrayList in Object Reference Confusion

Caglar Cataloglu
Greenhorn

Joined: Feb 06, 2010
Posts: 25
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?


Java Lover
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Why do you think it should be zero? (Please provide the running code.)
Jim ... ...


BEE MBA PMP SCJP-6
Caglar Cataloglu
Greenhorn

Joined: Feb 06, 2010
Posts: 25
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
Sheriff

Joined: Sep 28, 2004
Posts: 18977
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Caglar Cataloglu
Greenhorn

Joined: Feb 06, 2010
Posts: 25
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

Joined: Mar 22, 2010
Posts: 308



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!




SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
Caglar Cataloglu
Greenhorn

Joined: Feb 06, 2010
Posts: 25
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

Joined: Mar 22, 2010
Posts: 308

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

Joined: Feb 06, 2010
Posts: 25
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!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList in Object Reference Confusion