Java – What are the VB.NET static keywords for [Android] Java?

What are the VB.NET static keywords for [Android] Java?… here is a solution to the problem.

What are the VB.NET static keywords for [Android] Java?

Is there a Java equivalent for the Static keyword for VB.NET – especially on Android? For those unfamiliar with VB.NET, take a look at the code below fragment

….

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

The static keyword causes myvar to retain its value between subsequent calls to MyFinc.

Therefore, after three calls to MyFunc, the values for myvar will be: 1, 2, and 3.

How do I create a cross-call persistent variable in a Java method? May I?

Solution

No. In a method, Java has nothing to remember in various calls.

If you want to persist a value across multiple calls of a method, you should store it as an instance variable or class variable.

An instance variable

is different for each object/instance, while a class variable (or static variable) is the same for all objects of its 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
    }
}

Related Problems and Solutions