What is the [Android] Java equivalent of the VB.NET static keyword?

Is there a Java equivalent - especially on Android - for the VB.NET Static keyword? For those new to VB.NET, take the following code snippet ...

 Sub MyFunc() Static myvar As Integer = 0 myvar += 1 End Sub 

The Static keyword makes myvar keep its value between subsequent calls to MyFunc.

So, after each of the three calls to MyFunc, the value of myvar will be: 1 , 2 and 3 .

How do you create a cross-call variable in a Java method? Can you?

+4
source share
2 answers

No. Inside the method, Java does not have something that can be remembered in different calls.

if you want to save a value for several method calls, you must save it as an instance variable or class variable.

Instance variables are different for each object / instance, while class variables (or static variables) are the same for all objects in this class.

eg:

 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 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 } } 
+4
source

This is the static keyword in Java

 public static String mVar = "Some Value"; 
-one
source

Source: https://habr.com/ru/post/1392854/


All Articles