简体   繁体   中英

Is there a performance benefit to local variables vs. class/instance variables in Java (Android)?

I often define variables as class/instance variables ('global' before edit, thanks for the clarification) in my class while programming in Android. In case I need to access it later, say in another method after it's been assigned in onCreate() .

For the occasions where I don't actually access them later, Android Studio's Lint code inspection throws warnings stating that the "Field can be converted to a local variable".

I know I will get the same functionality either way, but is there any performance or security benefit to inline/local method variables in Java (specifically on Android, but also in general) vs. declaring them as private class/instance variables within a class?

EDIT/CLARIFICATION: By 'global', I meant in the scope of the class. (What I know understand to be referred to as 'class' or 'instance' variables, my bad) Accessible by all methods within the class and not an inline or method specific variable. Maybe a sort of example code will illustrate my point. EX:

public class MyActivity {

//Android Studio Lint will throw Java Class structure 'Field can be local' warnings 
//which is why I'm referring to these variables as "global" 
    private SomeDataType myPrivateVariable1; //'Field can be converted to a local variable'
    public SomeDataType myPublicVariable1; //'Field can be converted to a local variable'
    private SomeDataType myPrivateVariable2; 
    public SomeDataType myPublicVariable2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity_layout);

        //assign data to variables from either intent bundle or preferences or w/e
        myPrivateVariable1 = SOME_DATA;  
        myPublicVariable1 = SOME_DATA;
        //AS will suggest change to:
        // SomeDataType myPrivateVariable1 = SOME_DATA;
        // SomeDataType myPublicVariable1 = SOME_DATA;

        myPrivateVariable2 = SOME_DATA;
        myPublicVariable2 = SOME_DATA;

        //AS Lint will throw warning that variables can be converted b/c only used here
        someMethod(myPrivateVariable1);
        someOtherMethod(myPublicVariable1); 

        //run a method that uses the variables
        myMethodUsingVariable2(input);
    }

    private void myMethodUsingVariable2(DataType input) {
        //access the variables in multiple methods will not trigger the warning
        if (input == something) {
            //do something with 'myPrivateVariable2' and 'myPublicVariable2'
        }
    }

}

What is the performance benefit to this? If later I find I need to use either myPrivateVariable1 or myPublicVariable1 in another method as I add a feature or change something, it would be easier to write new methods that used the data if they were already saved to a defined class variable and assigned a value from the onCreate() method. Is the only benefit memory allocation that will only significantly affect performance if the variables are large data sets? What would be the difference between public and private in that regards as well?

I often define variables as global (private) in my class while programming in Android. In case I need to access it later, say in another method after it's been assigned in onCreate().

What you meant is a term for Class Scope variable.

I know I will get the same functionality either way, but is there any performance or security benefit to inline/local method variables in Java (specifically on Android, but also in general) vs. declaring them as private global variables within a class?

The major benefit using a method scope variable is maintainability . Consider the following class with class scope variable:

public class SampleClass {
  // a class scope variable
  private int mHeight;

  private int getSquareValueOfHeight() {
    return mHeight * mHeight;
  }

  private void increaseHeightByOne() {
    mHeight = mHeight + 1;
  }
}

we have two method; getSquareValueOfHeight() which read the value of mHeight and return the square value of it, and increaseHeightByOne() which modified the value of mHeight .

You can see that you need to check mHeight whenever you need to change both the methods. How about if there are 3 or 5 or more methods accessing the mHeight ? You have to recheck of all the methods just to make sure a change doesn't break your whole code.

Now consider the following class:

public class SampleClass {

  private int height;

  private int getSquareValueOfHeight(int value) {
    int height = value * value;
    return;
  }

  private int increaseHeightByOne(int height) {
    return height + 1;
  }

}

we have two methods that using a value from its parameter. The getSquareValueOfHeight() will return a squared value without modifying the class scope height variable because it has its own height variable (this is a shadowing mechanism). When you're calling the following code:

 SampleClass sampleClass = new SampleClass();
 int value = sampleClass.getSquareValueOfHeight(5);

the class scope variable height won't be changed. So, you don't need to worry that a change will broke your whole code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM