Static local variables not allowed in Java ?

R

Razvan

Hi !






Today I tried something like:


class Rebo {

int StaticLocalVar()
{
static int counter = 0;

return counter++;
}

}


And to my surprise the compiler complained because the counter is
static:

name/mihaiu/test/CTest.java:69: illegal start of expression
static int counter = 0;
^
1 error


What if you need to keep the counter value between calls to
StaticLocalVar() ? Like it is, the only option is to transform the
local variable in an attribute. (while removing the static qualifier)
But, if the variable has meaning only for the function itself ? Why
putting it in the class ? Is there another way ?




Regards,
Razvan
 
R

Roedy Green

What if you need to keep the counter value between calls to
StaticLocalVar() ? Like it is, the only option is to transform the
local variable in an attribute. (while removing the static qualifier)
But, if the variable has meaning only for the function itself ? Why
putting it in the class ? Is there another way ?

You have to make it a private static and just put a note it is not for
general use, just for method x. A year or so ago I proposed allowing
local statics as a language but not JVM extension. Nobody seemed to
think it was needed.
 
M

Michael Borgwardt

Razvan said:
What if you need to keep the counter value between calls to
StaticLocalVar()?

Java is not C. It's object-oriented. Objects, not local or global variables,
are the carriers of state.
 
J

John C. Bollinger

Razvan said:
class Rebo {
int StaticLocalVar()
{
static int counter = 0;
return counter++;
}
}


And to my surprise the compiler complained because the counter is
static:

name/mihaiu/test/CTest.java:69: illegal start of expression
static int counter = 0;
^
1 error

"Surprise" suggests that you had some reason to expect different. You
will likely avoid other surprises if you study the Java language a
little more thoroughly instead of relying on its similarities to other
languages you may have experience with.
What if you need to keep the counter value between calls to
StaticLocalVar() ? Like it is, the only option is to transform the
local variable in an attribute. (while removing the static qualifier)
But, if the variable has meaning only for the function itself ? Why
putting it in the class ? Is there another way ?

If the variable retains its value between method calls then it _is_ an
instance attribute (or possibly a class attribute) any way around.

The potential advantage to declaring a variable the way you would like
is in restricting its visibility to the only place it is intended to be
used. The disadvantages generally revolve around the manifold ways in
which the syntax is potentially confusing. For instance, where else
does "static" mean "not static" (as you point out, correctly, in this
case it should)? Will the semantics be different if used in a static
method rather than in an instance method? What if you want your method
to refer to a local-only variable that is in fact shared by all
instances (the standard meaning of "static" in Java) instead of one
specific to a particular class instance? Why make it harder than it
already is to guess whether and how a method may depend on class or
instance state?

Note also that bytecode does not directly support the visibility
distinction you wish to draw. This means, among other things, that a
"static local variable" would be visible (and possibly manipulable) via
reflection. It would have a status similar to that of the synthetic
methods generated by the compiler to support inner class access to
private methods and variables of their outer class(es). There is thus
precedent, but I consider that precedent to be a bit of a wart in the
first place.


John Bollinger
(e-mail address removed)
 
J

John C. Bollinger

Roedy said:
You have to make it a private static and just put a note it is not for
general use, just for method x. A year or so ago I proposed allowing
local statics as a language but not JVM extension. Nobody seemed to
think it was needed.

I not only think it is not needed, I think (as I detailed in another
post) that it has great potential for confusion. Moreover, I think that
in most places where a use for such a beast is conceived, it is
indicative that a big object would benefit from decomposition into
smaller objects (where it makes sense for the variable in question to be
a regular instance variable of one of those objects). This kind of
transformation can produce better abstractions, better thread safety,
and more easily maintainable code.


John Bollinger
(e-mail address removed)
 
R

Razvan

You will likely avoid other surprises if you study the Java language a
little more thoroughly instead of relying on its similarities to other
languages you may have experience with.


This is what I am trying to do, but after three years of C++
programming you get used to some things. When some basic things are
changed to feel the need to ask other people about it, just to be sure
that you got it right. Otherwise you can sustain some affirmations
only to find out latter that you have embarrassed yourself with your
ignorance.

The potential advantage to declaring a variable the way you would like
is in restricting its visibility to the only place it is intended to be
used.

Right.

Will the semantics be different if used in a static method rather than
in an instance method?

Certainly. When a static local variable is used in a static method it
is in fact a static attribute. But when a static local variable is
used in a non-static method then it is just a normal attribute
(non-static).

Ex:

void someFunc1()
{
static int ii; // this is like declaring the attribute ii in the
class
}

static void someFunc1()
{
static int ii; // this is like declaring the STATIC attribute ii in
the class
}


What if you want your method to refer to a local-only variable that is in fact > shared by all instances (the standard meaning of "static" in Java) instead of > one specific to a particular class instance?

You cannot do that with local variables. You must use instance
variables.


Why make it harder than it already is to guess whether and how a
method may depend on class or instance state?

Perhaps you are exagerrating a bit. It does not seems to be that
difficult.


You don't need to answer. From my point of view everything is clear
now.




Regards,
Razvan
 
R

Razvan

You will likely avoid other surprises if you study the Java language a
little more thoroughly instead of relying on its similarities to other
languages you may have experience with.


This is what I am trying to do, but after three years of C++
programming you get used to some things. When some basic things are
changed to feel the need to ask other people about it, just to be sure
that you got it right. Otherwise you can sustain some affirmations
only to find out latter that you have embarrassed yourself with your
ignorance.

The potential advantage to declaring a variable the way you would like
is in restricting its visibility to the only place it is intended to be
used.

Right.

Will the semantics be different if used in a static method rather than
in an instance method?

Certainly. When a static local variable is used in a static method it
is in fact a static attribute. But when a static local variable is
used in a non-static method then it is just a normal attribute
(non-static).

Ex:

void someFunc1()
{
static int ii; // this is like declaring the attribute ii in the
class
}

static void someFunc1()
{
static int ii; // this is like declaring the STATIC attribute ii in
the class
}


What if you want your method to refer to a local-only variable that is in fact > shared by all instances (the standard meaning of "static" in Java) instead of > one specific to a particular class instance?

You cannot do that with local variables. You must use instance
variables.


Why make it harder than it already is to guess whether and how a
method may depend on class or instance state?

Perhaps you are exagerrating a bit. It does not seems to be that
difficult.


You don't need to answer. From my point of view everything is clear
now.




Regards,
Razvan
 
T

Tor Iver Wilhelmsen

void someFunc1()
{
static int ii; // this is like declaring the attribute ii in the
class
}

static void someFunc1()
{
static int ii; // this is like declaring the STATIC attribute ii in
the class
}

Yes, but that's just C semantics interpreted to fit C++ concepts.
static method variables in C is a poor man's namespaces; real OO
languages use classes, packages/modules and other explicit namespaces.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top