1

I'm considering creating the following method:

public static MyBigCollection doSomeStuff(MyBigCollection m) { ... return m; }

and then in another method (perhaps in another class), using it like so:

MyBigCollection mbc = new MyBigCollection();
mbc = stuffClass.doSomeStuff(mbc);

Am I going about this the right way -- is this an efficient way to "do some stuff" to an object? I'd like to break off the stuff like so for extensibility. I've been doing c# for so long I'm just not sure with java. In c# the method could return void and I can simply call doSomeStuff(mbc) -- which would effectively pass my object by reference and do some stuff to it. I've been reading that java works differently so I wanted to check with the experts here.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
mikey
  • 5,090
  • 3
  • 24
  • 27
  • I don't think Java is much different from C# here. – Thilo Oct 25 '12 at 01:48
  • Thanks all. I realize this was a simple question, I was thrown off by another thread, because I didn't realize the poster was pointing out that setting the object to a new instance doesn't actually replace the original. http://stackoverflow.com/questions/40480/is-java-pass-by-reference – mikey Oct 25 '12 at 02:11

4 Answers4

2

I'd refactor to:

stuffClass.doSomeStuff(mbc);

(i.e., a method that modifies mbc and returns void)

Keep in mind that all Java Objects are stored in heap memory, and passed around with reference pointers.

Jiman
  • 185
  • 2
  • 13
2

The way you're doing it is fine, but you don't actually need to return the object at the end of the method. As such, the following would be simpler...

MyBigCollection mbc = new MyBigCollection();
stuffClass.doSomeStuff(mbc);

Objects in Java are passed by reference, so any modification on the mbc Object in the doSomeStuff() method would still be retained in the mbc variable after the end of the method call.

The only reason why you might consider returning the mbc Object is if you want the ability to join multiple methods together, such as this...

MyBigCollection mbc = new MyBigCollection();
mbc.doStuff1().doStuff2().doStuff3();

In this case, because mbc is returned by each of the doStuff() methods, the next method can be called straight back on to the same Object. Without returning the reference, you'd have to do something like this instead...

MyBigCollection mbc = new MyBigCollection();
mbc.doStuff1();
mbc.doStuff2();
mbc.doStuff3();

Which is the same thing, but not quite as compact. How you go about it really depends on how you intend to use the methods and the Object itself.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
  • technically objects are passed by value. that value happens to be an address on the heap where the data lives. Calling it "pass by reference" is misleading because that lends us to believe we can change the pointer inside the method and affect the pointer that the caller sees. This is not the case. – Matt Oct 25 '12 at 03:41
2

There's only one way to pass Java objects around. Java passes everything by value. Objects aren't passed; they live on the heap. You pass references around, not objects.

Same as C#, as far as I know.

This kind of micro-optimization is usually meaningless.

duffymo
  • 305,152
  • 44
  • 369
  • 561
1

I think your question is around how Java passes references to objects. Java passes by value, which can be confusing when first said. For objects, this means that the value of the reference to the object is passed to the method. Interacting with the object referred to by the value will alter the object 'passed in', so you don't need to return it.

Strings are treated differently as they are immutable. Primitives are also pass by value, but as the value passed is not a reference, you will not alter the original variable.

The easiest way to test this is to write some code and observe (you might also consider the Java tutorials)

Romski
  • 1,912
  • 1
  • 12
  • 27