-2

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?

Mr.Drew
  • 939
  • 2
  • 9
  • 30
  • The benefits of defining a variable in the tightest scope possible are many. Security is one of the benefits, but really as a consequence of making your code (and the lifetime of objects) easier to reason about. – Andy Turner Mar 30 '19 at 08:09
  • 1
    You might find this question related [Java local vs instance variable access speed](https://stackoverflow.com/questions/21613098/java-local-vs-instance-variable-access-speed). – Enzokie Mar 30 '19 at 08:13
  • There is no such thing as a global variable in Java. You are referring to either `static` or instance variables. Which is it? – user207421 Mar 30 '19 at 09:28
  • Please see my update for what I mean, I now understand that there is no true "global" variable in Java. Sorry for the confusion, I guess I meant is class/instance variables vs. inline ones. – Mr.Drew Mar 30 '19 at 11:53

1 Answers1

1

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.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
  • Yes, thanks for understanding what I meant by global before my edit. I am thinking the opposite for maintainability. For instance, if I want to update the class variable `height` based on user input of a number and use the `getSquareValueOfHeight()` method and another method `getHalfOfHeight()` that updates another field. Both would need access to same variable, so instead of calling them both with new input, I would just call the class variable which is updated in another method `changeHeight()`. – Mr.Drew Mar 30 '19 at 12:14
  • But still, is memory the only benefit then? Once the method executes the variable is garbage collected, but if it's a class variable it exists until Android destroys the class? Correct? – Mr.Drew Mar 30 '19 at 12:16