Win a copy of Production-Ready Serverless (Operational Best Practices) this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

swapping objects  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
friends,

i need to swap two java objects. Can anyone help me find out how?

MyClass class1 = new MyClass(10);
Myclass class2 = new MyClass(20);

swap(class1,class2);

private void swap(MyClass cls1, MyClass cls2){

//Howto write the swap method???

}

Thanks in advance/Jason
 
Ranch Hand
Posts: 2908
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well , thats the classic problem in Java world ..

"How to swap instance of a class ?"

I know the one solution regarding it , But not efficient ( I suppose !)

Steps :

1. make array of class "MyClass" ,of size two .
2. pass this array reference to swap method.
3. Swap this array element ,
4. and finally , return the same array ,
5. print values.

You got your original reference pointing to each others object , ie Swapped objects!

Try it .
and ranchers ,please correct me OR suggest any other method !
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is pass by value even for objects.

Thus you won't be able to swap objects referred by reference variables class1 & class2 using a method call swap().

You can probably do the following to swap two objects :


MyClass class1 = new MyClass(10);
Myclass class2 = new MyClass(20);

Myclass class3 = class1;

class1 = class2;
class2 = class3;

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by jason tan:
friends,

MyClass class1 = new MyClass(10);
Myclass class2 = new MyClass(20);

swap(class1,class2);

private void swap(MyClass cls1, MyClass cls2){

//Howto write the swap method???

}




 
Java Cowboy
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case you're wondering about Madhukars solution: It doesn't work, that's why he added a smiley to his post.

What you're asking is impossible in Java (at least if MyClass is immutable), because Java is pass-by-value.

See the JavaRanch campfire story Pass-by-Value Please to learn how this works.
 
To avoid criticism do nothing, say nothing, be nothing. -Elbert Hubbard. Please critique this tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!