1

why do some people make a new reference in method to a field variable?

public class Foo {
    int mFoo[] = {1, 2, 3};

    void method() {
        int foo[] = mFoo; // WHY not just use mFoo?

        print(foo[0]); // WHY not just use mFoo?
    }
}
4
  • 1
    Maybe they think its marginally faster (which it could be). Or maybe to avoid threading issues. Commented Jun 27, 2012 at 15:07
  • 1
    @scrappedcola if I change 'foo[0]=5' the same will also reflect to mFoo. Commented Jun 27, 2012 at 15:11
  • @Thilo Threading issues? Can you elaborate how that helps? Commented Jun 27, 2012 at 16:22
  • @EricB. see the accepted answer Commented Jun 27, 2012 at 22:53

6 Answers 6

5

It can be a good idea if for example, at another place of your code, you change the value of mFoo. Especially in a multithreaded context, you would want to ensure you're still working on the right data.

Sign up to request clarification or add additional context in comments.

2 Comments

if I would change 'foo[0]=5' it will also reflect on mFoo. So could you elaborate why this would be different in multithreaded context?
Imagine you are in a multithreaded app. In one of your thread, you begin to iterate on your table. In another thread, you execute mFoo=anotherTable; What happens next in your iterating thread?
3

It used to be the case (and still is with Java ME AFAIK) that loading a field into a local variable and using it multiple times was faster than access a field multiple times.

However, today using a local variable this way can confuse the JIT compiler and using a local variable this way can be slower.

2 Comments

Really? I can't imagine this slowing things down. (I have seen recent benchmarks demonstrating speedups with this technique even on Java 7 -server, but only tiny ones in extremely performance-critical code.)
This was a claim by one of the JVM developers less than a month ago. I imagine it depends on the situation. I haven't seen it myself. ;)
2

It depends on your goal. If you want to modify whatever Object was passed in as a parameter you would not make a new instance of the type of the referenced Object.

Otherwise you would make a new instance which would you could modify freely without worrying about modifications to the passed in Object. This is commonly used in multi-threaded applications.

1 Comment

Making new instances is not mentioned in the question, though.
2

There's an accepted answer already, but the use makes more sense in this context:

private Class<?> clazz = ...;

public void foo() {
    Class<?> current = clazz;
    while (current != Object.class) {
        // do some stuff.
        current = current.superclass();
    }
}

In this example you are constantly re-assigning "current" to a new value. Imagine the side effect without the variable, and if you kept doing:

clazz = clazz.superclass();

You'd be changing the field in the object itself, not locally to your method, so repeated calls to "foo" would not exhibit the behavior you are expecting.

1 Comment

the accepted answer was the first one that clarified me the issue.
1

It just doesn't make sense if you use just like this:

int[] local = somethingElse;
... // changes to local will reflect on somethingElse

It would make sense if a real copy would be done like:

int[] local = Arrays.copyOf(somethingElse,somethingElse.lenght())
... // changes to local will not reflect on somethingElse

The copy can prevent changing the elements that you will need on your method invoking other methods or by other Threads interference. In case that you have for example a method that changes this elements invoked at the same time you are using them. So, you can make a copy and work with them without being concern about the modification applied to it.

A test can be done with the following code:

int[] test = new int[] {1,2,3};
int[] test2 = Arrays.copyOf(test,test.length);
test2[0] = 9;
System.out.println(Arrays.toString(test));

Comments

0

In case of primitive types like int you're coping the values, not pointer, so your original array is still untouched after invoking method "print". It doesnt hold though for arrays

1 Comment

if we have int arrays then we are also just pointing and not copying. this would be copying: int i=5; int j=i;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.