views:

138

answers:

3

Hello ,
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 variable 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.

+6  A: 

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.

Bozho
Well, the scope in the same in C or C++, yet they allow static variable inside functions just fine.
doublep
Yes ! In C++,static/class variables can be declared inside a method.
Mandar
@doublep: yes, but Java is not C or C++, there's no reason why it should work the same in Java.
Jesper
@Jesper: Before being edited, the answer said sth. to the tune of "static for local variables don't make sense". I'm not saying Java is the same, I'm saying that static local variables do have their use even if they are not allowed in Java.
doublep
+1  A: 

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.

Donal Fellows
+1  A: 

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.)

Adam Robinson
@Adam: Ref to your note :As far as I know , you need to declare Bar( ) method as static because static methods can access only static data.
Mandar
@Mandar: That means that I have to declare the `bar` variable as static, not the `Bar()` method; you're correct that static methods can only access static data, but instance methods can also access static data.
Adam Robinson
@Adam : Yeah...
Mandar