Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why there is no local static variable in Java?

Tags:

java

oop

static

In C/C++ we use static local variables for maintaining a method's state. But why it is not supported in Java?

Yes, I can use an static field for this purpose. But isn't it a bit weird to create a field for maintaining only one method's state?

like image 407
Rafi Kamal Avatar asked Sep 05 '12 04:09

Rafi Kamal


People also ask

Can we have local static variable in Java?

Object Oriented Programming Fundamentals Unlike C, C++, Java does not allow static local variables. The compiler will throw the compilation error.

Why local variables are not static?

A local variable scope is within the method where it is declared. If the local variable is declared static, the meaning of static is lost. If the local variable is static, the purpose of static variable is bypassed. For this reason, compiler does not allow static local variables.

Can static variable be local?

A local static variable is a variable, whose lifetime doesn't stop with a function call where it is declared. It extends until the lifetime of a complete program. All function calls share the same copy of local static variables. These variables are used to count the number of times a function is called.

Can we create local variable in static method?

Local variables in static methods are just local variables in a static method. They're not static, and they're not special in any way. Static variables are held in memory attached to the corresponding Class objects; any objects referenced by static reference variables just live in the regular heap.


3 Answers

You have found the only solution.

Java dropped a number of complexities from C++, and this was one of them.

Static variables scoped to a function do nasty things to you in concurrency (e.g. strtok is a famously nasty one to use with pthreads, for exactly this reason).

In general, what you want is an object with state. The function in question should then have an object-level variable. Then you can create instances that each maintain state.

Much easier to understand/maintain/etc.

If you truly need to maintain state as a singleton, then static fields are it.

like image 152
Tony K. Avatar answered Oct 16 '22 23:10

Tony K.


The Java language spec doesn't seem to defend the omission of variables that correspond to C static variables.

Hiding state in class methods has a few drawbacks when seen from a Java perspective. Generally the existence of a function-level static variable isn't the sort of implementation detail that you'd want to expose outside of that function.

But the method's state is actually part of the class's state, and method-level static variables would have to be serialized / deserialized any time the object is persisted. This might not sound common, coming from a C background, so I'll note a few common examples.

  • Application server clusters can pass user session objects between nodes in order to provide fault tolerance.
  • JAXB could be used to marshall an object into an XML document
  • JPA can be used to persist object state to a database

If the variable's value is worth saving when the object is persisted, then there's a good chance that code outside of that class will need to reference that value. And suddenly that means defining access levels -- is a static variable in a public method automatically public? Or would a programmer have to declare it so?

We also have to think about extensibility. Would derived classes be required to implement the same static variable? Or would there be a reference to the variable from the function in the base class?

It's more likely that the C method that would use a static local variable would be a good candidate for a class in Java. It has state and hopefully exists for a single purpose. There's little drawback to encapsulating the functionality into an object, and it makes for a cleaner separation between transient values (such as local variables) and more long-term state.

like image 32
GargantuChet Avatar answered Oct 16 '22 22:10

GargantuChet


Some of the other answers show why you might not want to have this. But you can also ask why from a historical perspective.

To answer this you have to start to see why C does have static local variables. C has much fewer means than Java and C++ to limit the scope of a variable, the only options for static data are 'inside the file' and 'everywhere'. So this provides an extra layer, to limit the scope.

An important aspect of C++ is compatibility with, so it is allowed in C++ as well. But it doesn't need local static scope as much anymore, because there are many other means to limit scope of static data. The use is not popular in (modern) C++.

Java merely takes a lot of inspiration from C/C++, it didn't have to worry about backwards compatibility, so it could be left out.

like image 40
Thirler Avatar answered Oct 17 '22 00:10

Thirler