10

I have a question: let's say we have this function: (in C++)

int& f() {
    static int x = 0;
    return x;
} // OK

and

int& h() {
    int x=0;
    return x;
} // ERROR

Why does h give an error? Is it because of the keyword static? I found static keyword lets my x variable live after my function is terminated. So I still can access at that memory location from the outside (another function or main? right?). Instead int x = 0 is lost after h terminates. Right? I'm not sure I really got it!

And what about Java? I read I cannot declare static variables in methods but only in classes.

Thank you.

Jeffmagma
  • 452
  • 2
  • 8
  • 20
bog
  • 1,323
  • 5
  • 22
  • 34

4 Answers4

21

In C++, static is one of the most overloaded keywords of the language. The meaning you're using here is this:

A variable which is defined inside a function with the static specifier has static storage duration - it occupies the same space for the entire runtime of the program, and keeps its value between different calls to the function. So you can safely return a reference to it, as the variable is always there to back the reference.

A normal (non-static) function-local variable is destroyed when the function call returns, and so the reference becomes dangling - it doesn't refer to anything valid. Using it results in Undefined Behaviour.

Java simply doesn't have function-scope static variables (it doesn't have that meaning of the keyword static). That's why you can't declare it there.

Both C++ and Java have the "class-scope" meaning of the static keyword. When a member of a class is declared with the static keyword, it means the member is not bound to any instance of the class, but is just a global variable whose identifier lives in the class's scope.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • I would give a precision on C++ (or C) static variables. As the have only one space all along the program, they are non re-entrant : in multithreaded programs, they share a common value between all threads. And if you want to call the function in a recursive manner, they are neither pushed to stack nor popped. – Serge Ballesta Jun 18 '14 at 15:15
  • @SergeBallesta What exactly does a "re-entrant variable" mean? I always understood *code* to be re-entrant or thread-safe, not *data.* But you're of course right that a `static` local variable is just a global variable with funny name access rules. – Angew is no longer proud of SO Jun 18 '14 at 15:18
  • I agree with you an re-entrant variable does not mean anything. But when you have a static variable in a function, the function will have re-entrance problems ... – Serge Ballesta Jun 18 '14 at 16:15
3

Static keyword is used for almost same purpose in both C++ and Java. There are some differences though.

1) Static Data Members: Like C++, static data members in Java are class members and shared among all objects.

2) Static Member Methods: Like C++, methods declared as static are class members and have following restrictions:

  • (i) They can only call other static methods.
  • (ii) They must only access static data.
  • (iii) They cannot access this or super

Like C++, static data members and static methods can be accessed without creating an object. They can be accessed using class name.

3) Static Block: Unlike C++, Java supports a special block, called static block (also called static clause) which can be used for static initialization of a class. This code inside static block is executed only once (first time you make an object of that class or the first time you access a static member of that class (even if you never make an object of that class)).

4) Static Local Variables: Unlike C++, Java doesn’t support static local variables. If used , Java program fails in compilation.

5) Static class: Classes can also be made static in Java.In java, we can’t make Top level class static. Only nested classes can be static.

  • Nested static class doesn’t need reference of Outer class, but Non-static nested class or Inner class requires Outer class reference.
  • Inner class(or non-static nested class) can access both static and non-static members of Outer class. A static class cannot access non-static members of the Outer class. It can access only static members of Outer class.

Ref : www.geeksforgeeks.org

Rogue
  • 11,105
  • 5
  • 45
  • 71
roottraveller
  • 7,942
  • 7
  • 60
  • 65
  • 2
    Verbatim repetition of this webpage: http://www.geeksforgeeks.org/static-keyword-in-java/ Are you the author of this article or have you plagiarized? If the former, say so in your post so we will know. – Kallaste Apr 21 '17 at 02:04
  • agreed - possible plagiarism – LeanMan Oct 05 '20 at 07:43
0

Static variables in C++ in functions are persistant varibles in the scope of a function. In Java, you can't have static variables in methods. Static variables are variables of the class, not its instances.

Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
  • @jaunchopanza, it's sort-of right for the case of variables declared `static` at function scope. It's incomplete, however, since it ignores other uses of the heavily overloaded `static` keyword to qualify variables in other scopes. – 0xbe5077ed Jun 18 '14 at 15:04
  • @juanchopanza Sorry, I specified that that's only when declared in a function. Is that correct? – Anubian Noob Jun 18 '14 at 15:15
0

For the Java side you are correct. Static variables in Java must be declared at the class level not inside a method.

If you need to scope the static variables then you probably have some seriously broken architecture but you can do it to a certain extent by using inner classes to store the static variables in.

Tim B
  • 40,716
  • 16
  • 83
  • 128