开发者

Local variables in java

I went through local variables and class variables concept.

But I had stuck at a doubt

" Why is it so that we cannot declare local variables as static " ?

For e.g

Suppose we have a play( ) function :

void play( )  
{  
  static int i=5;  
  System.out.println(i);  
}

It gives me error in eclipse : Illegal modifier for parameter i;

I had this doubt because of the following concepts I have read :

  1. Variables inside method : scope is local i.e within that method.
  2. When varia开发者_开发问答ble is declared as static , it is present for the entire class i.e not to particular object.

Please could anyone help me out to clarify the concept.

Thanks.


Because the scope of the local variables is limited to the surrounding block. That's why they cannot be referred to (neither statically, nor non-statically), from other classes or methods.

Wikipedia says about static local variables (in C++ for example):

Static local variables are declared inside a function, just like automatic local variables. They have the same scope as normal local variables, differing only in "storage duration": whatever values the function puts into static local variables during one call will still be present when the function is called again.

That doesn't exist in Java. And in my opinion - for the better.


Java doesn't have static variables like C. Instead, since every method has a class (or instance of a class) associated with it, the persistent scoped variables are best stored at that level (e.g., as private or static private fields). The only real difference is that other methods in the same class can refer to them; since all those methods are constrained to a single file anyway, it's not a big problem in practice.


Static members (variables, functions, etc.) serve to allow callers of the class, whether they're within the class or outside of the class, to execute functions and utilize variables without referring to a specific instance of the class. Because of this, the concept of a "static local" doesn't make sense, as there would be no way for a caller outside of the function to refer to the variable (since it's local to that function).

There are some languages (VB.NET, for example), that have a concept of "static" local variables, though the term "static" is inconsistently used in this scenario; VB.NET static local variables are more like hidden instance variables, where subsequent calls on the same instance will have the previous value intact. For example

Public Class Foo
    Public Sub Bar()
        Static i As Integer

        i = i + 1

        Console.WriteLine(i)
    End Sub
End Class

...

Dim f As New Foo()
Dim f2 as New Foo()

f.Bar() // Prints "1"
f.Bar() // Prints "2"
f2.Bar() // Prints "1"

So, as you can see, the keyword "static" is not used in the conventional OO meaning here, as it's still specific to a particular instance of Foo.

Because this behavior can be confusing (or, at the very least, unintuitive), other languages like Java and C# are less flexible when it comes to variable declarations. Depending on how you want it to behave, you should declare your variable either as an instance variable or a static/class variable:

If you'd like the variable to exist beyond the scope of the function but be particular to a single instance of the class (like VB.NET does), then create an instance variable:

public class Foo
{
    private int bar;

    public void Bar()
    {
        bar++;

        System.out.println(bar);
    }
}

If you want it to be accessible to all instances of the class (or even without an instance), make it static:

public class Foo
{
    private static int bar;

    public static void Bar()
    {
        bar++;

        System.out.println(bar);
    }
}

(Note that I made Bar() static in the last example, but there is no reason that it has to be.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜