4

Is there a Java equivalent - specifically on Android - for VB.NET's Static keyword? For those not familiar with VB.NET, take the following code snippet...

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.

So after each of three calls to MyFunc, myvar's value would be: 1, 2 and 3 .

How do you make a cross-call persistent variable within a method in Java? Can you?

eidylon
  • 7,068
  • 20
  • 75
  • 118
  • 1
    java also has 'static' keyword and it serves the same purpose – Waqas Jan 25 '12 at 06:46
  • 1
    From what I gathered, Java's `static` is more akin to VB.NET's `Shared`... denoting that a class member does not require a specific class instance; no? Or does Java's `static` actually serve dual purpose of both VB.NET's `Shared` and its `Static`? – eidylon Jan 25 '12 at 06:48
  • in java/c# member declared as static can be used without instantiating an instance and it also shared among different instances thus retaining it's value between subsequent calls. – Waqas Jan 25 '12 at 06:57
  • 2
    But you cannot use `static` in the same way in Java as in eidylon's VB example. You cannot make local variables `static` in Java. – Jesper Jan 25 '12 at 08:33

2 Answers2

4

No. Within a method, Java doesn't have something which can be remembered across various calls.

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
    }
}
Azodious
  • 13,752
  • 1
  • 36
  • 71
  • So I would need to move my variable outside of the method and put it at the class level then, yes? Then I can declare my method and my variable both as `static` and it would have roughly the same effect then? That would be the closest thing? – eidylon Jan 25 '12 at 06:58
  • 2
    yes, you should move variable at class level. however method need not be `static`. a non-static method can access `static` variable. but remember that others method can also see the value changed by one method. – Azodious Jan 25 '12 at 07:09
-1

It is static keyword in Java

public static String mVar = "Some Value";
Rajdeep Dua
  • 11,190
  • 2
  • 32
  • 22