java (beginner) : returning an object ? is it returned as a constant reference or what?

60,461

Solution 1

In Java, You always return a reference (unless returned value is a primitive type such as int, float, char, ...).

So, if you don't want the returned object to be modified, you must return a full copy of it (you could use Clonable interface and clone method if your class defines it).

Solution 2

So, to answer your questions you have to at first know how Java passes Variables.

a Variable has a value:

int i = 1234;
Person p = new Person("Peter");

Now, the Variable i contains exactly 1234, while the Variable p contains the Memory Adress of the created Person.

so i contains 1234 and p contains the adress (let's say a4dfi3).

anyMethodYouLike(p);
System.out.println(p.getName());
public void anyMethodYouLike(Person somePerson) {
   somePerson.rename("Homer");
}

so in this example, we give the Method anyMethodYouLike the Variable p... wait! we give the Method the value of the Variable (a4dfi3). The Method then calls rename on this Variable (which still has the same adress as p has, hence it modifies the same Person that p points to). So, after the Method, the Name of the Person p points to, gets printed, which results in "Homer".

someOtherMethod(p);
System.out.println(p.getName());
public void someOtherMethod(Person somePerson) {
   somePerson = new Person("Walter");
}

In THIS example we still give the adress of our Person called "Peter" to the Method. But this time, the Method creates a new Person in somePerson (therefore overriding the adress in somePerson to.. let's say 13n37s. BUT! the Person at a4dfi3 wasn't changed! The print call still outputs "Peter" and not "Walter".

Now, let's see how this behaves with primitives:

someMethod(i);
System.out.println(i);
public void someMethod(int someInt) {
   someInt++;
}

So, the Value of i (1234) gets passed to someInteger. Then someInteger gets incremented to 1235. But i is still 1234.

This is the big difference between Objects and primitives in Java.

Now to your questions: 1. As you can read here, yes Java always passes the Reference Adress of the Object. 2. If you don't want someone to mess with the values of your Objects, you HAVE to first create a new Object with that information (e.g with Cloneable and clone()), but it's a real mess because you have to make sure, that everything in your Object that is not primitive gets re-created, which is just awful when you have huge Tree-structures of Objects.

I hope I could help, Ferdi265

Solution 3

What you should really realize is that there is nothing special about "passing" or "returning". Whenever you "pass" or "return" something, it just passes the value of the thing. Period. For any type. Whenever you "pass" or "return" something, it's exactly the same as simply assigning it to a variable.

However, what is the value of the thing you are passing or returning? That is where your confusion seems to lie. Your question asks about "returning an object"; however, such a thing does not make sense in Java. Objects are not values in Java. It is impossible to have a variable whose value is an object.

The only types in Java are primitive types and reference types. Hence the only values are primitives and references. (References are pointers to objects.) In Java, we only manipulate objects through references (pointers to objects). We cannot store an object in a variable; but we can store a reference (pointer to an object) in a variable. So when you talk about passing or returning objects, you are almost certainly instead talking about passing or returning references. And, as said before, there is nothing special about passing or returning references -- the value of the reference (a pointer) is passed or returned.

Solution 4

It returns the object's reference.

say suppose you have a method call like.

Object obj = makeObject();

which creates an Object and returns(which is the reference of the object created in the makeObject method).

Share:
60,461
Ismail Marmoush
Author by

Ismail Marmoush

You can check my website and blog https://marmoush.com

Updated on August 05, 2022

Comments

  • Ismail Marmoush
    Ismail Marmoush almost 2 years

    I have a function that returns a user-defined object. First I want to know if that object is returned by reference and what if it was private?

    Also, how do I return it as Constant (final) reference because I don't want someone to mess with it? I'm so confused between returning an object and returning object.copy(); or object.clone();

  • Michael Borgwardt
    Michael Borgwardt over 13 years
    ...or the object's designed to be immutable.
  • Pablo Santa Cruz
    Pablo Santa Cruz over 13 years
    @Michael Borgwardt: great point. I think adding it to the answer will confuse the person asking the question though...
  • bigblind
    bigblind over 10 years
    is an array passed by reference? As far as I know, it's a primitive type, but it is mutable.