无法在Java中访问方法之外的方法局部变量 [英] Unable to access a method's local variables outside of the method in Java
本文介绍了无法在Java中访问方法之外的方法局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Java的新手,正在尝试访问方法外部的方法变量,但这不起作用.
I am new to Java and am trying to access method variables outside of the method, but it doesn't work.
代码如下:
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.
这篇关于无法在Java中访问方法之外的方法局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文