-1

I know that C++ supports static variables in a method and in java, static members are shared among all objects.but why this code fails to compile in java?

class Learn
{
 static int count = 0;
 Learn(int n)
 {
    count+=n;
 }
 public void method()
 {
    static int count =0;
 }
}
public class th
{
  public static void main(String a[])
  {
    Learn l = new Learn(4);
  }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
kalimba
  • 408
  • 4
  • 14

4 Answers4

1

It doesn't allow you to initialize static variables inside methods because variables initialized methods are destroyed after the method has been called, and a static variable is not only accessible outside the method but outside the class. Those two ideas are conflicting.

jhobbie
  • 1,016
  • 9
  • 18
  • It helps :) but why it is allowed in c++ ? – kalimba Jul 30 '14 at 18:16
  • Well, I don't know C++, so I can't help you there. Are variables declared inside methods accessible outside of the method? – jhobbie Jul 30 '14 at 18:17
  • @kalimba I believe it is an artifact from C, where there are no classes. It was a way for a function to have state without creating globals. – clcto Jul 30 '14 at 18:25
  • @clcto: You can write perfectly good modern C++ code without any class. C++ supports various programming paradigms. – Christian Hackl Jul 30 '14 at 18:29
  • @ChristianHackl I disagree. I don't think you can write modern C++ code without `std::string` or the STL. – clcto Jul 30 '14 at 18:53
  • 1
    @clcto: Well, but those are not classes in the OOP sense. You don't derive from them, and they have no virtual functions. In fact, most modern C++ has little to do with inheritance and virtual functions. And the STL is, at its heart, a system of interacting concepts, compatible with arrays and pointers. – Christian Hackl Jul 30 '14 at 19:07
0

Java doesn't allow you to declare a static local variable, such as the count you're declaring in method. If you want to initialize the static class variable, then in method, replace

static int count =0;

with

count = 0;
rgettman
  • 176,041
  • 30
  • 275
  • 357
  • Yeah i know that but my question is 'why that so?' is there a difference between the way in which static variables are used in c++ and java? – kalimba Jul 30 '14 at 18:15
  • There is no such concept as a `static` local variable in Java. Java only allows the `final` modifier on local variable declarations, and nothing else such as `static`. – rgettman Jul 30 '14 at 18:17
  • This would not work the same way as C++. Count would be 0 every time method() is called, not just the first time, which I believe the OP wants. – Ted Bigham Jul 30 '14 at 18:18
0

In java, you declare the static variable outside the method to have the same effect as declaring it static inside the method in C++.

I know that using static outside a method in C++ gives the variable "file scope", but java is different.

If you need a few lines of code to initialize the variable, then use an anonymous static method to initialize it when the class loads

static {
    count = 0;
}
Ted Bigham
  • 4,237
  • 1
  • 26
  • 31
  • Not sure "anonymous static method" is a thing. I think they call it a static initializer, or static block informally. – som-snytt Jul 30 '14 at 18:28
  • Agreed, "static initializer" is the proper term for sure. Without an example the term is easy to confuse with a method used for something like "static count = initCount()". So "anonymous static method" is my description, not the term. – Ted Bigham Jul 30 '14 at 22:22
0

Unlike C++, Java doesn’t support static local variables. Because In Java, a static variable is a class variable (for whole class). All methods are declared inside the class, Its not possible to declare member function or any variables outside a class. Even main() method should be inside a class. So if we have static local variable (a variable with scope limited to function), it violates the purpose of static. Hence compiler does not allow static local variable.