[英]What is [Android] Java's equivalent of VB.NET's Static Keyword?
Is there a Java equivalent - specifically on Android - for VB.NET's Static
keyword? 是否存在Java等价物 - 特别是在Android上 - 用于VB.NET的
Static
关键字? For those not familiar with VB.NET, take the following code snippet... 对于那些不熟悉VB.NET的人,请使用以下代码片段...
Sub MyFunc()
Static myvar As Integer = 0
myvar += 1
End Sub
The Static
keyword makes it so myvar retains its value between subsequent calls to MyFunc. Static
关键字使得myvar在后续MyFunc调用之间保留其值。
So after each of three calls to MyFunc, myvar's value would be: 1
, 2
and 3
. 所以,每经过三个调用MYFUNC,MYVAR的价值将是:
1
, 2
和3
。
How do you make a cross-call persistent variable within a method in Java? 如何在Java中的方法中创建交叉调用持久变量? Can you?
你能?
No. Within a method, Java doesn't have something which can be remembered across various calls. 在一个方法中,Java没有可以通过各种调用记住的东西。
if you want to persist a value across multiple calls of a method, you should store it as instance variable or class variable. 如果要在方法的多个调用中保持值,则应将其存储为实例变量或类变量。
Instance variables are different for each object/instance while class variables (or static variables) are same for all the objects of it's class. 每个对象/实例的实例变量是不同的,而类变量(或静态变量)对于它的所有对象是相同的。
for example: 例如:
class ABC
{
int instance_var; // instance variable
static int static_var; // class variable
}
class ABC_Invoker
{
public static void main(string[] args)
{
ABC obj1 = new ABC();
ABC obj2 = new ABC();
obj1.instance_var = 10;
obj2.instance_var = 20;
ABC.static_var = 50; // See, you access static member by it's class name
System.out.prinln(obj1.instance_var);
System.out.prinln(obj2.instance_var);
System.out.prinln(ABC.static_var);
System.out.prinln(obj1.static_var); // Wrong way, but try it
System.out.prinln(obj2.static_var); // Wrong way, but try it
}
}
它是Java中的静态关键字
public static String mVar = "Some Value";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.