tomi
tomi

Reputation: 535

referencing class variable to a local variable

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?
    }
}

Upvotes: 1

Views: 124

Answers (6)

Matt
Matt

Reputation: 11805

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.

Upvotes: 2

Francisco Spaeth
Francisco Spaeth

Reputation: 23903

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));

Upvotes: 1

iga
iga

Reputation: 21

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

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533510

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.

Upvotes: 3

John Kane
John Kane

Reputation: 4443

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.

Upvotes: 2

Alexis Dufrenoy
Alexis Dufrenoy

Reputation: 11946

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.

Upvotes: 5

Related Questions