I am new to Java and am trying to access method variables outside of the method, but it doesn't work.
Code is below:
public class MethodAccess {
public static void minus() {
int a=10;
int 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;
}
}
解决方案
The variables that are defined inside a method are local to that method, so you cannot use them outside. If you want to use them outside, define instance variables in the beginning of your class.