java 如何在java中从一个函数访问一个变量到另一个函数?可以在函数中将该局部变量设为静态吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2085323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access a variable from one function to another function in java ? Can that local variable be made static in a function?
提问by giri
Possible Duplicate:
How do I create a static local variable in Java?
可能的重复:
如何在 Java 中创建静态局部变量?
Hi I am noob to java I wanna access local variable of a function in another function. But java does java support static variable in a function? If not then how to access that variable.I do not want to make that local variable as instance variable of calss.
嗨,我是 Java 的菜鸟,我想访问另一个函数中函数的局部变量。但是java在函数中支持静态变量吗?如果不是,那么如何访问该变量。我不想将该局部变量作为 calss 的实例变量。
Thanks
谢谢
回答by John Doe
Maybe I'm totally wrong but the task sounds to me like a part of closures concept (even if authod didn't mean anything related).
也许我完全错了,但这个任务对我来说听起来像是闭包概念的一部分(即使 authod 并不意味着任何相关的东西)。
So, java way is:
所以,java方式是:
public void foo () {
final SomeClass obj = new SomeClass ();
Bar b = new Bar () {
public void bar () {
obj.doSmth ();
}
};
}
Of course, this concrete code is just an unuseful example.
当然,这个具体的代码只是一个无用的例子。
回答by witzar
In Java you say "method" instead of "function".
What do you mean by "static variable in a function"? If you mean C/C++ style static local variable, then you have to use class or instance variable, depending on your needs.
"Local" means that something cannot be accessed outside it's scope. So you can't access method's local variable from outside of the method body, just by definition.
And why can't you use class or instance variable?
在 Java 中,您说的是“方法”而不是“函数”。
“函数中的静态变量”是什么意思?如果您的意思是C/C++ 风格的静态局部变量,那么您必须根据需要使用类或实例变量。
“本地”意味着无法在其范围之外访问某些内容。因此,根据定义,您无法从方法主体外部访问方法的局部变量。
为什么不能使用类或实例变量?
回答by cheesysam
In the declaration of your method where you need to access the variable, include it as a parameter.
在需要访问变量的方法的声明中,将其作为参数包含在内。
public int myMethod(int i){
System.out.println(i);
}
And then pass the variable to that method as a parameter.
然后将变量作为参数传递给该方法。
public static void main(String [] args){
int myInt = 5;
myMethod(myInt);
}
回答by glenatron
It depends but there are a few ways.
这取决于但有几种方法。
If you're in the same object you could use a normal member variable, if you're calling between objects you could just provide getters and setters for that variable. Alternately if the two methods are related just provide the value as a parameter.
如果您在同一个对象中,您可以使用普通成员变量,如果您在对象之间调用,您可以只为该变量提供 getter 和 setter。或者,如果这两种方法相关,只需提供值作为参数。
When you talk about between one function and another function it's not clear whether you mean methods in the same class or between different classes. Maybe if you're thinking about functions in Java and not thinking about classes, objects and methods, you would find it useful to go over the basic object orientation stuff as well. I know I had that problem often when I was making the transition from procedural to object-oriented programming. It's a while ago now, but this series of tutorialshelped me a lot with that.
当您在一个函数和另一个函数之间谈论时,不清楚您指的是同一类中的方法还是不同类之间的方法。也许如果您正在考虑 Java 中的函数而不是考虑类、对象和方法,您会发现了解基本的面向对象内容也很有用。我知道当我从过程编程过渡到面向对象编程时,我经常遇到这个问题。这已经是很久以前的事了,但是这一系列的教程对我帮助很大。
回答by GuruKulki
No you cannot declare a static variable in a method in java.
不,您不能在 java 的方法中声明静态变量。
and you can not access a variable from one method in another method. rather if it is an object you can send its reference to other method through a method call. and if its a primitive then you can send its value.
并且您无法从另一种方法中的一种方法访问变量。相反,如果它是一个对象,您可以通过方法调用将其引用发送到其他方法。如果它是一个原语,那么你可以发送它的值。

