If you are trying to update a local variable declared within the main
method from outside of the method, then I'm afraid you can't.
public static void main(String[] args) {
int counter = 1;
doSomething(); // there is no way that 'doSomething()' can update 'counter'
}
Java has neither first-class closures or the ability to pass the address of a variable as a parameter (i.e. call by reference). These are the two approaches that other languages typically use to implement out-of-scope mutation of local variables.
But the fact that you are trying to do this suggests that you are missing something important about OO programming and design. You should rewrite your code to do one or more of:
- return the value or values as a result or an object that holds the results
- pass an object as a parameter and set the values in the object
- use a static variable
- use a shared object that is set up using dependency injection
(Static variables are a poor choice ... for various reasons ... and DI is too complicated for a beginner, and entails a lot of dependencies.)