I am new to Java and am trying to access method variables outside of the method, but it doesn\'t work.
Code is below:
You need to define the variables as static class variables, so you can access them from a static function. Also watch out for the access modifiers, since when the variable is private you can't access them outside any other class.
public class methodacess {
private static int a;
private static int b;
public static void minus(){
methodacess obj =new methodacess();
a=10;
b=15;
}
public static void main (String[] args){
//Here i want to access variable names a & b that is in minus()
int c = b - a;
}
}